How to remove leading whitespace from each line in a file

Lazer picture Lazer · Feb 22, 2010 · Viewed 136k times · Source

I have a file that looks something like this:

for (i = 0; i < 100; i++)
    for (i = 0; i < 100; i++)
  for (i = 0; i < 100; i++)
       for (i = 0; i < 100; i++)
     for (i = 0; i < 100; i++)
           for (i = 0; i < 100; i++)
for (i = 0; i < 100; i++)

I want it to look like this (remove indentations):

for (i = 0; i < 100; i++)
for (i = 0; i < 100; i++)
for (i = 0; i < 100; i++)
for (i = 0; i < 100; i++)
for (i = 0; i < 100; i++)
for (i = 0; i < 100; i++)
for (i = 0; i < 100; i++)

How can this be done (using sed maybe?)?

Answer

shuvalov picture shuvalov · Feb 22, 2010
sed "s/^[ \t]*//" -i youfile

Warning: this will overwrite the original file.