Tuesday, December 10, 2013

Some useful SED command syntaxes

To delete trailing whitespaces at the end of each lines:
# sed 's/[ \t]*$//' filename > filename_notrailingspace

To remove all blank lines in a given file:
# sed '/^$/d' filename > filename_noblankspace

To remove all leading and trailing whitespaces of each lines in a given file:
$ cat filename | sed 's/^[ \t]*//;s/[ \t]*$//' > filename_nospace

1 comment:

  1. Good tips. Replacement using sed are really powerful.

    Reading the second hint it came to my mind a command I frequently use to remove blank lines and comments from output of a configuration files for reading purpose.

    cat /path/to/file/file.conf | egrep -v "^$|^#"

    This way you don't modify the file.