Computing.Net > Forums > Unix > korn shell help

korn shell help

Reply to Message Icon

Original Message
Name: alanevenden
Date: May 31, 2007 at 21:53:34 Pacific
Subject: korn shell help
OS: Solaris
CPU/Ram: N/A
Model/Manufacturer: N/A
Comment:

I'm trying to write a korn shell script and I'm at my wits end as to how to solve it!

The problem I have is that I'm trying to parse a file and edit specific parts of the file.

For example, I have 4 re-occurring lines of data with 2 variables I'm interested in:

blah blah blah VAR1 blah
.......................
......................
blah blah blah blah VAR2

I need the script to match VAR1 and VAR2 to specified critera, which when matched - I then need to sed VAR2 with VAR3

e.g

I live in a town
............
............
and I drive a car
I live in a town
............
............
and I drive a truck

if town is found in line 1, and truck is found in line 4, then replace truck with lorry

I've tried everything I can think of using grep and gnu grep, piping into sed, but can't solve the problem - please help!

I know you guys are proper clever - all help is much appreciated =D


Report Offensive Message For Removal

Response Number 1
Name: lankrypt0
Date: June 1, 2007 at 08:01:41 Pacific
Subject: korn shell help
Reply: (edit)

Can the word town be anywhere on the 1st line and the word truck anywhere on the 4th?


Report Offensive Follow Up For Removal

Response Number 2
Name: lankrypt0
Date: June 1, 2007 at 08:12:33 Pacific
Subject: korn shell help
Reply: (edit)

If position of the words doesnt matter, this will find the word truck 3 lines after the word town and replace it with lorry:

grep -A3 town infile|tail -1|grep truck|sed -e "s/truck/lorry/g"


Report Offensive Follow Up For Removal

Response Number 3
Name: alanevenden
Date: June 4, 2007 at 17:43:35 Pacific
Subject: korn shell help
Reply: (edit)

Unfortunately this doesn't seem to work for me, the file isn't edited =(.

To make things clearer, I'm using korn on Solaris 9 to script, and here is an example of the actual text in a file I'm looking to edit:

<ManagedElementId string="$VAR1"/>
<primaryType type="xxx"/>
<managedElementType types=""/>
<associatedSite string="Site=$VAR2"/>

I want to leave the file exactly as it is, except for when VAR1 and VAR2 are found in this scenario and then I wish to substitute VAR2 with VAR3


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: korn shell help

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software