Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting lines into a file

Status
Not open for further replies.

hcclnoodles

IS-IT--Management
Jun 3, 2004
123
GB
Hi there

Probably an easy question to answer, but how can i add/remove particular lines of a text file from a shell script, for example i have a text file called text.txt which has 3 lines of text and i would like to insert those 3 lines into lines 7,8 and 9 of mainfile.txt (which is somewhere in the middle of the file, not at the end)

on the other side, how can i issue a command from the shell that will remove lines 7,8 and 9 from main.txt

any help on this would be greatly appreciated

 
1) insertion
echo "6r test.txt\nwq" | ex -s mainfile.txt
2) deletion
echo "7,9d\nwq" | ex -s main.txt

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks PHV, but i cannot get that to work at all, i issue the command (making sure the file names are correct) and it returns me to the next line of the shell with no errors, but nothing happens to the files...they stay the same

i tried running

ex -s mainfile.txt

from the commadline and it returns me to a new line with no prompt ....? i tried entering

6r test.txt\nwq
:wq

and it does quit me out but still nothing changes

I am running Fedora Core 1 with VIM - Vi IMproved version 6.2.531
 
try:
/bin/echo "6r test.txt\nwq" | ex -s mainfile.txt

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
nope still no joy, however if i manually run

#ex mainfile.txt (without the -s silent)

and type

:6r test.txt\nwq

i get..

:6r test.txt\nwq
E484: Can't open file test.txtnwq

....but if i run just

:6r test.txt

....the operation is successful and the file is edited

so i tried adding a space between test.txt and \nwq ie ...

:6r test.txt \nwq

but i get

E172: Only one file name allowed



any ideas ??

 
for some flavours of Unix you will need -e option:
echo -e "6r test.txt\nwq" | ex -s mainfile.txt
hth
 
strange - as posted it works fine un Sun/Solaris/ksh

what OS and what shell are you running?

try this:
Code:
(/bin/echo '6r test.txt'; /bin/echo 'wq') | ex -s mainfile.txt

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
i have a solaris box here and it does indeed work on that (just tested), however the box i am trying to get working is a fedora core linux using BASH


ps the -e thing didnt work

but

(/bin/echo '6r test.txt'; /bin/echo 'wq') | ex -s mainfile.txt

thankyou for all your help
 
sorry i didnt explan that properly

(/bin/echo '6r test.txt'; /bin/echo 'wq') | ex -s mainfile.txt worked fine, thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top