User Tools

Site Tools


sed:remove_whitespace_from_a_file

This is an old revision of the document!


SED - Remove whitespace from a file

Delete whitespace from begin of each line

cat input.txt | 's/^[ \t]*//' > output.txt

Delete trailing whitespace from end of each line

cat input.txt | sed 's/[ \t]*$//' > output.txt

Remove leading and trailing whitespace from each line

cat input.txt | sed 's/^[ \t]*//;s/[ \t]*$//' > output.txt

Remove all whitespace (including tabs) from beginning of string

echo "     This is a test" | sed -e 's/^[ \t]*//'

result:

This is a test

Where,

  • s/ : Substitute command ~ replacement for pattern (^[ \t]*) on each addressed line
  • ^[ \t]* : Search pattern ( ^ – start of the line; [ \t]* match one or more blank spaces including tab)
  • ** : Replace (delete) all matched pattern —-
sed/remove_whitespace_from_a_file.1576002649.txt.gz · Last modified: 2020/07/15 09:30 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki