1826325 Members
3552 Online
109692 Solutions
New Discussion

help editing file

 
SOLVED
Go to solution
Jeanine Kone
Trusted Contributor

help editing file

I have a file that I am trying to edit. Basically, it has a variable amount of spaces at the end of each line. I want to get rid of these spaces and replace them with a single ^.
There are a lot of lines, so I am looking for a single command that I can run once.

Another way to put it is that I want to go to the last non-blank character of each line of the file and replace everything after it with a ^.

Thanks!

7 REPLIES 7
Robin Wakefield
Honored Contributor
Solution

Re: help editing file

Hi,

sed 's/ *$/^/' filename

should do it.

rgds, Robin
James R. Ferguson
Acclaimed Contributor

Re: help editing file

Hi Jeanine:

# sed -e 's/[ \t][ \t]*$/^/' filename

Note that you need to type a TAB character in place of the '\t'. HP's 'sed' doesn't understand the notation. Hence that's a SPACE and a TAB character within each of the brackets.

Regards!

...JRF...
Sridhar Bhaskarla
Honored Contributor

Re: help editing file

Hi,

Do the following

sed 's/ /^/g' your_file > new_file

(it is 's//^/g')

Or vi the file and type in

:%s/ /^/g


If you have tabs, then you can use the following

sed 's/[ ]/^/g' your_file
> new_file

('s/[]/^/g)

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Pete Randall
Outstanding Contributor

Re: help editing file

Hi Jeanine,

For future reference, I'm attaching the Handy One-Liners for Sed that I originally got from Princess Paula.


Pete

Pete
Jeanine Kone
Trusted Contributor

Re: help editing file

Thanks Guys.

Robin and James. They did exactly what I needed. For anyone else that might read this in the future - you need to do a "> newfile" to put the changes into a new file, otherwise, it just scrolls across the screen.

Sri - These commands replace the spaces with ^ one for one - not just the ones at the end of the file as I wanted. Thanks for trying.
H.Merijn Brand (procura
Honored Contributor

Re: help editing file

Jeanine, if you are confident enough to do it in one swoop without temp file, you can use perl's in-line change. It modifies the file itself

# perl -pi -e 's/\s+$/^/' file

If you are less confident, you can create a backup

# perl -pi.bkp -e 's/\s+$/^/' file


Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
H.Merijn Brand (procura
Honored Contributor

Re: help editing file

uhh, forgot the -l option. \s is also a newline, meaning that after the inline edit the whole file would be reduced to a single line, something you do not want

pc03:/tmp 503 $ banner help >xx
pc03:/tmp 504 $ perl -pi -e 's/\s+$/^/' xx
pc03:/tmp 505 $ cat xx
^^ XX XX^ X X^ X X^ X XX XX
XXX X XXXXXX^ XX X X X X X X^ X X XXXXXXX X X
X^ X X X X X X^ X X X X X X X^ XXX XXX
XXXXX XXXXX XXXXX^ X^ XXX
^pc03:/tmp 506 $ banner help >xx
pc03:/tmp 507 $ perl -pi -le 's/\s+$/^/' xx
pc03:/tmp 508 $ cat xx
^
^
XX XX^
X X^
X X^
X XX XXXXX X XXXXXX^
XX X X X X X X
X X XXXXXXX X X X
X X X X X X
X X X X X X X
XXX XXX XXXXX XXXXX XXXXX^
X^
XXX^
pc03:/tmp 509 $

So replacing -e with -le will help a lot here.

Enjoy, have FUN! H.Merijn [ Who thinks that this message will be near to unreadable due to the space compressing in the forum messages ]
Enjoy, Have FUN! H.Merijn