sábado, 7 de febrero de 2015

sed Tip: Delete All Blank White Spaces


by on June 9, 2013 · 5 comments· LAST UPDATED June 9, 2013
I have a text file as follows:
   foo
   bar
  foobar 
How can I delete all leading and/or trailing blank spaces, tab from each line using sed command?

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
Requirementssed/awk/perl
Estimated completion time2 minutes
You can use sed command to delete all white (blank) spaces in a text file. You can also use other text processing utilities such as:
  1. Perl.
  2. Python.
  3. Awk and friends.

Perl example

The syntax is:
perl -lape 's/\s+//sg' input > output
Sample outputs:
foo
bar
foobar
Or use -pie syntax to update file:
 
cat input
perl -lapi -e 's/\s+|^\n//sg'  input
cat input
 
See perl man page for more information.

Sed example

The syntax is:
sed -e 's/^[ \t]*//' -e 's/[ \t]*$//' input > output
OR updated file in a single go with the -i option:
sed -i -e 's/^[ \t]*//' -e 's/[ \t]*$//' input
See sed command man page for more information.

Awk example

The syntax is
awk '{$1=$1}{ print }' input > output
You can also use gsub() substring matching the regular expression function. See awk man page for more information.

http://www.cyberciti.biz/faq/sed-tip-delete-all-blank-white-spaces/

No hay comentarios:

Publicar un comentario