1833871 Members
1771 Online
110063 Solutions
New Discussion

Re: Script help

 
Greg Stark_1
Frequent Advisor

Script help

Hello,

I need help with a script that I want to add a line to an existing file in a certain spot. For instance, take the following file:

NAME "server_DLT"
DESCRIPTION "server_DLT"
HOST server.com
POLICY Standalone
TYPE DLT
POOL "server_DLT"
CLEANME
DRIVES
"/dev/rmt1"

and change it to:

NAME "server_DLT"
DESCRIPTION "server_DLT"
HOST server.com
POLICY Standalone
TYPE DLT
POOL "server_DLT"
EJECT
CLEANME
DRIVES
"/dev/rmt1"

i.e. add the EJECT line after the POOL line. Any help would be greatly appreciated.

Thanks in advance,
Greg
5 REPLIES 5
Scott Robertson
Occasional Advisor

Re: Script help

Greg,

This works for me, assuming that there is only one keyword POOL. Also, be warned, everytime you run this it will keep adding the word EJECT after POOL.

#!/bin/sh
ed filename </^POOL /
a
EJECT
.
w
q
STOPHERE
*formally known as srobertson@bcbsga.com
Rodney Hills
Honored Contributor

Re: Script help

From perl you can do the following

perl -n -i -e 'print $_; print "EJECT\n" if /^POOL/' yourscriptfile

This will process the file and add EJECT after the line beginning with POOL
There be dragons...
Bernie Vande Griend
Respected Contributor

Re: Script help

Or just use sed:

sed '/CLEANME/iEJECT' copyoffile > filename
Ye who thinks he has a lot to say, probably shouldn't.
Bernie Vande Griend
Respected Contributor

Re: Script help

hmm, looks like it takes out backslashes.
There should be a backslash after the /i
and CHANGE should be on the next line.
Ye who thinks he has a lot to say, probably shouldn't.
James R. Ferguson
Acclaimed Contributor

Re: Script help

Hi Greg:

Here's how I would do this:

#!/usr/bin/sh
echo "/POOL/a\\\\\nEJECT" > /tmp/sed.cmd
sed -f /tmp/sed.cmd /tmp/inp > /tmp/out
mv /tmp/out /tmp/inp
#.end.

The echo creates a sed script (command) file that looks like this:

/POOL/aEJECT

The 'a' says "append" after the match is made with "POOL".

Regards!

...JRF...