Andrew
Andrew

Reputation: 309

sed : how remove selected number of white spaces (tabs) from beginning of each line

I have some python sample code with 6 white useless spaces in the beginning of each line.

I have this code to remove all white spaces...

sed 's/^[ \t]*//;s/[ \t]*$//'

But how to remove just specified number of white spaces ( in my case 6) ?

Upvotes: 0

Views: 249

Answers (2)

anubhava
anubhava

Reputation: 784948

Converting my comment to answer so that solution is easy to find for future visitors.

This sed should work for you (based on your comments):

sed -E 's/^[[:blank:]]{19}//;s/[[:blank:]]+$//' file

To save changes inline:

sed -i.bak -E 's/^[[:blank:]]{19}//;s/[[:blank:]]+$//' file

Upvotes: 1

Timur Shtatland
Timur Shtatland

Reputation: 12347

Use this Perl one-liner (for 19 whitespaces, per your comment):

perl -pe '^\s{19}' in_file > out_file

Or to change the file in-place:

perl -i.bak -pe '^\s{19}' in_file

The Perl one-liner uses these command line flags:
-e : Tells Perl to look for code in-line, instead of in a file.
-p : Loop over the input one line at a time, assigning it to $_ by default. Add print $_ after each loop iteration.
-i.bak : Edit input files in-place (overwrite the input file). Before overwriting, save a backup copy of the original file by appending to its name the extension .bak.

SEE ALSO:
perldoc perlrun: how to execute the Perl interpreter: command line switches
perldoc perlrequick: Perl regular expressions quick start

Upvotes: 0

Related Questions