Uses for ‘sed’

I’ve lost count of the number of times I’ve had to check how to remove extra spaces from the end of the lines, or convert between DOS and Unix style line feeds so I thought I’d better write down some of my useful shortcuts here!

Convert lines terminated with CFLF (DOS/Windows) to LF (Unix).

# sed -i 's/\r$//g' <filename>

Remove extra spaces from the end of lines.

# sed -i 's/ *$//g' <filename>

Remove line numbers from the beginning of each line in a file (this assumes that the line numbers being six digits long, right justified, and followed by a tab, giving a total of seven characters).

# cat <filename>|sed 's/^.\{7\}//'

Advertisements