'How to remove leading whitespace from each line in a file
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?)?
Solution 1:[1]
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 2:[2]
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 3:[3]
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 4:[4]
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
Solution 5:[5]
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
Solution 6:[6]
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
Solution 7:[7]
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! =)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Peter Mortensen |
| Solution 3 | Peter Mortensen |
| Solution 4 | Peter Mortensen |
| Solution 5 | Sheikh Wasiu Al Hasib |
| Solution 6 | Joao da Silva |
| Solution 7 | Peter Mortensen |
