Gregg's MOTD

Tips & Tricks that I've Encountered Over the Years...

Some sed Tips

July 06, 2023 — Gregg Szumowski

Put an * between the number 5 and 6

$ echo '12345678901234567890'| sed -e 's/^.\{5\}/&*/'
12345*678901234567890

Put a comma between each number

$ echo "1234567890" |sed -e 's/./&,/g' -e 's/,$//'
1,2,3,4,5,6,7,8,9,0

Put comma after every 2, if not even then last number exist by itself

$ echo "1234567890" |sed -e 's/../&,/g' -e 's/,$//'
12,34,56,78,90

Double space the data in file data1

$ sed G data1

Triple space the data file file data1

$ sed 'G;G' data1

Single space a double spaced file

$ sed 'n;d' data1

DOS to Linux conversion

$ sed 's/.$//' DOSfile >Linuxfile

Truncate everything after a substring (‘with’ in this case):

$ cat file.txt
Brown Potatoes with Cheese
Yellow Potatoes with Sugar and Cheese
$ sed -i.bak 's/with.*//g' file.txt
$ cat file.txt
Brown Potatoes
Yellow Potatoes

Tags: cli, sed, motd