Friday, May 22, 2009

Remove leading white space in vi editor


Well it sounds very simple, but thought would be useful for all vi newbies :-)

This is how we can remove all leading white space(s) from all lines in vi editor:


In ex mode:

:1,$ s/^\s*//g



Related post:

- Delete blank lines in vi editor
- More vi editor tips post from unstableme here

13 comments:

Unknown said...

%s/^\s*//g is a tad shorter and does the same.

Regards
Fake

Unknown said...

@Fake51, true. Thanks for the comment.

Alex said...

if using the form:

%s/^\s*//g

you don't need the 'g'. 'g' tells the search and replace to do so globally (on the current line) while '%s' tells the search and replace to do so on every line in the file.

Since each line can have only 1 occurrence of leading white space, the 'g' is unnecessary, since s/// will replace the first occurrence of the given pattern on the line.

Unknown said...

@Alex, you are right. I never realized that. Thanks for your comment :-)

Nandinho said...

Hi all; I´m new into unix, lets say I have a file called test.txt with this:

C, 123 456 4
A, 2344 56 5
Q , 12233 567

WHAT I want is remove all the spaces between the numbers and letters. Please can you help.
retagi

Unknown said...

@Retagi,

something like this ?

$ sed 's/ //g' test.txt
C,1234564
A,2344565
Q,12233567

As you said you are new to UNIX, let me guide you some more:

You can write the modified o/p in a new file :

$ sed 's/ //g' test.txt > test.txt.mod

$ cat test.txt.mod
C,1234564
A,2344565
Q,12233567

or if your sed supports -i option:

$ sed -i.bak 's/ //g' test.txt

$ cat test.txt
C,1234564
A,2344565
Q,12233567

$ cat test.txt.bak
C, 123 456 4
A, 2344 56 5
Q , 12233 567

(http://unstableme.blogspot.com/2010/01/sed-save-changes-to-same-file.html)

Nandinho said...

Hi, Lets say I have a file like this:
MSIEKIKINDA3A8INDA4IND
64301210562171841B4A30752C7F5E0C37431B2146220DC100
643012107417808EC1770A2CC290BCD4E0873786A70BB63100
64301210742179318F226C09BFC4230A865694138CFCC7C100
64301210800000000C9A6DE0D639842648C24F2F6B7B457100
64301210800000147A28301E5AE981EDBBC6D2416BF8EC4100

and I want to insert a space after teh 15th character of each line. How do I do it? Your help is appreciated.

regards
Fernando

Unknown said...

@Fernando,

If you need a space after first 8 characters of each line:

$ sed 's/......../& /' file.txt
MSIEKIKI NDA3A8INDA4IND
64301210 562171841B4A30752C7F5E0C37431B2146220DC100
64301210 7417808EC1770A2CC290BCD4E0873786A70BB63100
64301210 742179318F226C09BFC4230A865694138CFCC7C100
64301210 800000000C9A6DE0D639842648C24F2F6B7B457100
64301210 800000147A28301E5AE981EDBBC6D2416BF8EC4100

or If you need a space after every 8 characters of each line:

$ sed 's/......../& /g' file.txt
MSIEKIKI NDA3A8IN DA4IND
64301210 56217184 1B4A3075 2C7F5E0C 37431B21 46220DC1 00
64301210 7417808E C1770A2C C290BCD4 E0873786 A70BB631 00
64301210 74217931 8F226C09 BFC4230A 86569413 8CFCC7C1 00
64301210 80000000 0C9A6DE0 D6398426 48C24F2F 6B7B4571 00
64301210 80000014 7A28301E 5AE981ED BBC6D241 6BF8EC41 00

You can refer this post for other alternatives:
http://unstableme.blogspot.com/2009/09/insert-after-certain-characters-awk-and.html

harisadu said...

Hi What Could I do if I have to substitute the first occurence of space with comma in every line of a file.

Currently File is Something like this:

A B,C D E,F,G

Intended file should look like this

A,B,C D E,F,G

Unknown said...

@ssdd,

In vi ex mode
:%s/ /,/

Similarly

:%s/ /,/g
will chnge it to:
A,B,C,D,E,F,G

Using sed:

$ echo "A B,C D E,F,G" | sed 's/ /,/'
A,B,C D E,F,G

$ echo "A B,C D E,F,G" | sed 's/ /,/g'
A,B,C,D,E,F,G


Hope this helps. Thanks for reading unstableme.blogspot.com

Unknown said...

could use ur some help- I have a file which contains the 900+ lines like this:
uswmnswoowa02r-wawood01 -8
uswmnswoowa02r-wawood03 -6
uswmnswoowa02r-wawood10 -5
uswmnswoowa02r-wawood04 -6
uswmnswoowa02r-wawood08 -5

I need the format to look like this:
update|o|uswmnswoowa02r-wawood01|timezone|-08:00

So each line needs update|o| added in at the beginnning of each line.
|timezone| added after device name
and finally the -5 formatted to -05:00

Your help is much appreciated!

Unknown said...

@Syed, thanks for the question.

quickly I can try something like this:

$ awk '{
one=substr($0,1,23)
rest=substr($0,25)
printf ("update|o|%s|timezone|%s:00\n", one, rest)
}' file.txt

update|o|uswmnswoowa02r-wawood01|timezone|-8:00
update|o|uswmnswoowa02r-wawood03|timezone|-6:00
update|o|uswmnswoowa02r-wawood10|timezone|-5:00
update|o|uswmnswoowa02r-wawood04|timezone|-6:00
update|o|uswmnswoowa02r-wawood08|timezone|-5:00

Please let me know if I need to try some other alternative.

Ajay said...

All the above options are technically correct and this post proved to be a good collection of commands. However, I normally use below set commands to remove leading spaces

Case 1:
If only you want to chop leading spaces for one particular line, then place cursor on that particular line and run below command.

:le

Case 2:
To remove leading spaces from ever lines in a file, then run below command

:%le

© Jadu Saikia www.UNIXCL.com