Process Text Streams Using Sed on Linux

Yet another stream editor cheatsheet. 

GNU Sed One Liners

We’re using the following file for examples:

$ cat file.txt
one two one two one two one two
one two one two one two one two
ONE TWO ONE TWO ONE TWO ONE TWO

Delete a Line Containing a Specific String

$ sed '/one/d' file.txt
ONE TWO ONE TWO ONE TWO ONE TWO

Replace the First Occurence of a String on Each Line

$ sed 's/one/ONE/' file.txt
ONE two one two one two one two
ONE two one two one two one two
ONE TWO ONE TWO ONE TWO ONE TWO

Replace a Specific Occurrence of a String in a File

$ sed 's/one/ONE/' file.txt
ONE two one two one two one two
ONE two one two one two one two
ONE TWO ONE TWO ONE TWO ONE TWO
$ sed 's/one/ONE/3' file.txt
one two one two ONE two one two
one two one two ONE two one two
ONE TWO ONE TWO ONE TWO ONE TWO
$ sed 's/one/ONE/g' file.txt
ONE two ONE two ONE two ONE two
ONE two ONE two ONE two ONE two
ONE TWO ONE TWO ONE TWO ONE TWO
$ sed 's/one/ONE/3g' file.txt
one two one two ONE two ONE two
one two one two ONE two ONE two
ONE TWO ONE TWO ONE TWO ONE TWO

Make All Letters Uppercase

$ sed 's/./\U&/g' file.txt
ONE TWO ONE TWO ONE TWO ONE TWO
ONE TWO ONE TWO ONE TWO ONE TWO
ONE TWO ONE TWO ONE TWO ONE TWO

Make All Letters Lowercase

$ sed 's/./\L&/g' file.txt
one two one two one two one two
one two one two one two one two
one two one two one two one two

Add Word at the Beggining of Each Line

$ sed 's/^/START /' file.txt
START one two one two one two one two
START one two one two one two one two
START ONE TWO ONE TWO ONE TWO ONE TWO

Add Word at the End of Each Line

$ sed 's/$/ END/' file.txt
one two one two one two one two END
one two one two one two one two END
ONE TWO ONE TWO ONE TWO ONE TWO END

Remove All Empty Lines

$ sed '/^$/d'

Remove All Empty Lines Containing Spaces and Tabs

$ sed '/^\s*$/d'

List All Files in a Current Directory Excluding Some Extensions

$ ls -1 | sed '/csv\|txt\|log/d'

Converting from Unix-style ($) to DOS-style (\r)

$ sed s/$/"\r"/ dosfile.txt > unixfile.txt

Remove The First Character from Every Line

$ sed s'/^.//'

Remove the Last Character from Every Line

$ sed s'/.$//'

Delete All Lines That are Less Than 6 Characters Long

$ sed '/.\{6\}/!d'

Replace Newlines with a Comma

This will read the whole file in a loop, then replace the newlines with a comma.

$ sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/,/g' file.txt