Operating System - HP-UX
1820390 Members
3493 Online
109623 Solutions
New Discussion юеВ

how to remove or replace whitespace from file in VI editor?

 
importatnt
Occasional Contributor

how to remove or replace whitespace from file in VI editor?

Hi,

how to remove or replace whitespace from file in VI editor?

Thanks,
4 REPLIES 4
Tim Nelson
Honored Contributor

Re: how to remove or replace whitespace from file in VI editor?

:%s/\ //g

James R. Ferguson
Acclaimed Contributor

Re: how to remove or replace whitespace from file in VI editor?

Hi:

I assume you want to remove *trailing* whitespace (spaces and/or tabs) from the end of lines:

%s/[\s\t]*$//

...where '\s' is a space-character and for '\t' type a tab character.

Regards!

...JRF...
Bill Hassell
Honored Contributor

Re: how to remove or replace whitespace from file in VI editor?

White space is not just spaces, it also includes tabs (and in some programs like tr and awk, the whitespace concept includes the end of line character LF). Now the question is: do you want to delete all space and tab characters? If so, just use tr like this:

tr -d '[:blank:]' myfile

Redirect the output into a new file if you need to save the results. To get rid of spaces, tabs and LF characters, use:

tr -d '[:space:]' myfile

For other space manipulation, you can reduce all strings of more than one space to a single space with:

tr -s '[:space:]' myfile


Bill Hassell, sysadmin
Suraj K Sankari
Honored Contributor

Re: how to remove or replace whitespace from file in VI editor?

Hi,
Open your file in vi
at vi command prompt :
type

:1,$s///gp

Suraj