remove white space in bash using sed

Sal picture Sal · Apr 21, 2015 · Viewed 43.1k times · Source

I have a file that contains a number followed by a file path on each line for a large amount of files. So it looks like this:

      7653 /home/usr123/file123456

But the problem with this is there is 6 empty white spaces before it which throws off the rest of my script. I have included the line which produces it below:

cat temp | uniq -c | sed 's/  */ /g' > temp2

I have narrowed it down to being the uniq command that produces the unnecessary white space. I have tried to implement a sed command to delete the white space but for some reason it deletes all but one. How would I be able to modify my sed statement or my uniq statement to get rid of these white spaces? Any help would be greatly appreciated!

Answer

John1024 picture John1024 · Apr 21, 2015

To remove all the leading blanks, use:

sed 's/^ *//g'

To remove all leading white space of any kind:

sed 's/^[[:space:]]*//g'

The ^ symbol matches only at the beginning of a line. Because of this, the above will leave alone any whitespace not at the beginning of the line.