Operating System - OpenVMS
1752600 Members
4308 Online
108788 Solutions
New Discussion юеВ

Re: Usage of "ed" command in UNIX shell script

 
Ajay Saini
Frequent Advisor

Usage of "ed" command in UNIX shell script

Hello,

I am using the following command in one of my scripts(UNIX shell script):-
ed ./file >>EOF
/^EVENTS
i
EVENTS=101 102 152 201
.
w
q
EOF

Now is there a way to use the ed command in the above format to replace a line with another line in a file
7 REPLIES 7
Joseph Huber_1
Honored Contributor

Re: Usage of "ed" command in UNIX shell script

I think a Unix , specifically a ED forum will get You better answers.
But well: the current ED sequence is searching for the string EVENTS at the beginning of a line.
Then inserts a new line.

Replace the i command by a s/old string/new string/.
I'm not ED user, but if I remember well the s command stands for "substitute".

Use "man ed" on Your system to get more precise info.
http://www.mpp.mpg.de/~huber
labadie_1
Honored Contributor

Re: Usage of "ed" command in UNIX shell script

Use GNV
http://gnv.sourceforge.net/

, or Sed
http://vms.process.com/scripts/fileserv/fileserv.com?SED

, or Awk
AWK == "$ SYS$COMMON:[SYSHLP.EXAMPLES.TCPIP.SNMP]GAWK.EXE"

to do that.

So you want to automate

edit a file, go to the line beginning with EVENTS, insert
EVENTS=101 102 152 201
save and quit.
Ajay Saini
Frequent Advisor

Re: Usage of "ed" command in UNIX shell script

Hello Joseph,

Your command does work.
But I am unable to use a variable in this command, Like

var=/var/tmp --->a variable

ed ./file >>EOF
/^FILE
s/^FILE=.*/FILE=$var/
.
w
q
EOF

How can I do this????

And can you provide me the path for some good UNIX ED forums...
Joseph Huber_1
Honored Contributor

Re: Usage of "ed" command in UNIX shell script


ed probably does not evaluate or even recognize shell variables, $var is just part of the data stream, the shell does not see it. (same as on VMS). If You want to use variables, build the data in a temporary file instead of the input stream.
(and no, I can't help You with this, here is VMS land!)

And no, I don't know a specific news group for ed.
Eventually try comp.editors for (Unix) editor sopecific questions.
http://www.mpp.mpg.de/~huber
Joseph Huber_1
Honored Contributor

Re: Usage of "ed" command in UNIX shell script


And BTW, with sed the whole stuff is much easier:

sed s/oldstring/newstring/g filein > fileout

Since I'm no unix user, I leave the regexp syntax for s(ubstitution) string to You for Your case, but it certainly can contain shell variables, substituted by the shell before it reaches sed.
http://www.mpp.mpg.de/~huber
Dennis Handly
Acclaimed Contributor

Re: Usage of "ed" command in UNIX shell script

>UNIX shell script

What OS are you running? HP-UX, Linux or Tru64?

>Joseph: ed probably does not evaluate or even recognize shell variables

The shell does if they are in a here document.
Hoff
Honored Contributor

Re: Usage of "ed" command in UNIX shell script

Dennis: Or OpenVMS. Yes, OpenVMS can run shell scripts.