1824169 Members
3449 Online
109668 Solutions
New Discussion юеВ

Insert text vi question.

 
SOLVED
Go to solution
Amado A. Diaz Jr.
Occasional Advisor

Insert text vi question.

I want to insert text in the beginning of each line of a file. I also want to insert text at the end of each line of a file. Does anyone have any suggestions?

Any advice would be greatly appreciated.

Thank you.
-Amado
6 REPLIES 6
Hazem Mahmoud_3
Respected Contributor
Solution

Re: Insert text vi question.

In vi:

:%s/^//g
:%s/$//g

-Hazem
Rodney Hills
Honored Contributor

Re: Insert text vi question.

If it is the same text, then

:1,$s/^/new/
:1,$s/$/new2/

Will put "new" at beginning and "new2" at end.

HTH

-- Rod Hills
There be dragons...
Maxpert IT Service Cent
Occasional Advisor

Re: Insert text vi question.

If it is always the same text at the start and end of each line you could do the following:

awk '{print "start text", $0, "end text"}' yourfile > newfile
(check newfile)
mv newfile yourfile

hope this helps
Nicolas Dumeige
Esteemed Contributor

Re: Insert text vi question.

Well using previous answer ;D

=> using awk inside vi

:1,$!awk '{ "NEW"$0"NEW2" }'

Cheers

Nicolas
All different, all Unix
Jean-Luc Oudart
Honored Contributor

Re: Insert text vi question.

and if you want to repeat the operation for many files, you can run vi silently

1) create a command file "mycom"
:1,$s/^/NEW1/
:1,$s/$/NEW2/
:wq

2) run the command file on
vi /dev/null

You can loop this for a filelist if required

Regards
Jean-Luc
fiat lux
Amado A. Diaz Jr.
Occasional Advisor

Re: Insert text vi question.

THANK YOU!!!!!!!