Delete whitespace in each begin of line of file, using bash

G-71 picture G-71 · Feb 16, 2012 · Viewed 61.6k times · Source

How i can delete whitespace in each line of file, using bash For instance, file1.txt. Before:

  gg g
 gg g
t  ttt

after:

gg g
gg g
t  ttt

Answer

Scharron picture Scharron · Feb 16, 2012

sed -i 's/ //g' your_file will do it, modifying the file inplace.

To delete only the whitespaces at the beginning of one single line, use sed -i 's/^ *//' your_file

In the first expression, we replace all spaces with nothing. In the second one, we replace at the beginning using the ^ keyword