How to remove leading whitespace from each line in a file

146,196

Solution 1

sed "s/^[ \t]*//" -i youfile

Warning: this will overwrite the original file.

Solution 2

For this specific problem, something like this would work:

$ sed 's/^ *//g' < input.txt > output.txt

It says to replace all spaces at the start of a line with nothing. If you also want to remove tabs, change it to this:

$ sed 's/^[ \t]+//g' < input.txt > output.txt

The leading "s" before the / means "substitute". The /'s are the delimiters for the patterns. The data between the first two /'s are the pattern to match, and the data between the second and third / is the data to replace it with. In this case you're replacing it with nothing. The "g" after the final slash means to do it "globally", ie: over the entire file rather than on only the first match it finds.

Finally, instead of < input.txt > output.txt you can use the -i option which means to edit the file "in place". Meaning, you don't need to create a second file to contain your result. If you use this option you will lose your original file.

Solution 3

You can use AWK:

$ awk '{$1=$1}1' file
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++)

sed

$ sed 's|^[[:blank:]]*||g' file
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++)

The shell's while/read loop

while read -r line
do
    echo $line
done <"file"

Solution 4

This Perl code edits your original file:

perl -i -ne 's/^\s+//;print' file

The next one makes a backup copy before editing the original file:

perl -i.bak -ne 's/^\s+//;print' file

Notice that Perl borrows heavily from sed (and AWK).

Solution 5

Use:

sed -e **'s/^[ \t]*//'**  name_of_file_from_which_you_want_to_remove_space > 'name _file_where_you_want_to_store_output'

For example:

sed -e 's/^[ \t]*//'  file1.txt > output.txt

Note:

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 patterns

Share:
146,196
Lazer
Author by

Lazer

Updated on July 05, 2022

Comments

  • Lazer
    Lazer almost 2 years

    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?)?

  • shuvalov
    shuvalov about 14 years
    it will be shorter with -i(in-place) arg
  • 2rs2ts
    2rs2ts almost 11 years
    Your sed example worked for me where answers to other similar questions did not work. Thanks!
  • Per Lundberg
    Per Lundberg over 10 years
    FWIW, I couldn't get this (or anything similar) working on OS X Mavericks, now when I needed it. Running "perl -pe '<regexp>'" worked fine, though. I even tried adding the -E parameter to sed, which didn't help.
  • olala
    olala about 10 years
    @ghostdog74 what is this kinda of expression ([[:blank:]]) called? I saw another example where people used [[:space:]] to remove leading whitespaces. Besides, blank, space, what other words can be used here? I just want to understand this usage. Thanks!
  • Chris Warth
    Chris Warth about 10 years
    I can confirm that the sed on OS X Mavericks is broken w.r.t. every other unix-like OS. It does not appear to allow matching of any escaped character inside character classes.
  • Wei Qiu
    Wei Qiu almost 10 years
    You may install gsed on OS X.
  • user5359531
    user5359531 about 8 years
    @olala I found this in the man pages for tr; [:blank:] all horizontal whitespace, [:space:] all horizontal or vertical whitespace. So, if you use [[:space:]] it should not only remove the spaces and tabs, but also the newlines (\n) leaving you with a single line of text. Using [[:blank:]] will leave the newlines. Note that I haven't actually tried this with sed yet, but that is the action using tr
  • wisbucky
    wisbucky over 6 years
    You can use ANSI-C quoting $' ' on mac osx for it to interpret \t correctly. Must be single quotes, not double. sed $'s/^[ \t]*//' Also, mac osx's sed is not gnu, so a lot of the gnu features are not supported. @per @chris
  • Ben
    Ben over 6 years
    Default sed on Mac expects a temp file to use for inline replacements. You can generally just add '' after -i to satisfy this. The final command would be, sed -i '' 's/\s*//' yourfile
  • Jesse
    Jesse almost 5 years
    Just sayin', you are the first programmer I ever saw who used -i AFTER the RegEx arguments. That either proves that I am an insect among men or that you are superhuman. Frankly, I'm not sure which, but I feel like it's the latter. Thank you! Thank you! Thank you!
  • Jesse
    Jesse almost 5 years
    Not quite the direct answer, but solved the problem I was searching for. Thanks!
  • Ben
    Ben over 3 years
    Prometheus, was your comment here intended for a different answer?