76

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lazer
  • 90,700
  • 113
  • 281
  • 364

8 Answers8

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

Warning: this will overwrite the original file.

Ron Gejman
  • 6,135
  • 3
  • 25
  • 34
shuvalov
  • 4,713
  • 2
  • 20
  • 17
  • 3
    FWIW, I couldn't get this (or anything similar) working on OS X Mavericks, now when I needed it. Running "perl -pe ''" worked fine, though. I even tried adding the -E parameter to sed, which didn't help. – Per Lundberg Nov 14 '13 at 06:58
  • 3
    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. – Chris Warth Apr 10 '14 at 18:56
  • 4
    You may install gsed on OS X. – Wei Qiu Jul 31 '14 at 11:44
  • 8
    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 – wisbucky Sep 01 '17 at 00:08
  • 4
    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` – Ben Nov 02 '17 at 18:46
  • 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 Jun 27 '19 at 05:17
32

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.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
17

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"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
  • Your `sed` example worked for me where answers to other similar questions did not work. Thanks! – 2rs2ts Jun 10 '13 at 21:35
  • @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! – olala Feb 24 '14 at 17:18
  • 1
    @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` – user5359531 Apr 13 '16 at 18:08
  • This is the command which worked for me on MacOS. Thanks for sharing. – Raymond Jan 21 '23 at 16:34
5

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chris Koknat
  • 3,305
  • 2
  • 29
  • 30
2

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Zahid
  • 380
  • 6
  • 14
1

You can remove leading white space by below any command:

 - sed -i "s/\s*//" filename
 - sed -i "s/[[:space:]]*//"  filename        
 - sed -i "s/^\s*//"   filename
 - sed -i "s/[ \t]*//"  filename

If you want to remove blank line you can try any one of the following command:

 - sed "/^$/d"  filename

If you want to delete any leading white space, tab or empty line in that case you can use below command:

sed  -i -e "/^$/d"   -i -e "s/^\s*//"  filename
0

Here you go:

user@host:~$ sed 's/^[\t ]*//g' < file-in.txt

Or:

user@host:~$ sed 's/^[\t ]*//g' < file-in.txt > file-out.txt
Joao da Silva
  • 7,353
  • 2
  • 28
  • 24
0

For what it's worth, if you are editing this file, you can probably highlight all the lines and use your un-tab button.

  • In Vim, use Shift + V to highlight the lines, then press <<
  • If you're on a Mac, then you can use Atom, Sublime Text, etc., then highlight with your mouse and then press Shift + Tab

I am not sure if there is some requirement that this must be done from the command line. If so, then :thumbs-up: to the accepted answer! =)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ben
  • 1,620
  • 18
  • 11