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!

Complex Search/Replace 1

Status
Not open for further replies.

motoslide

MIS
Oct 30, 2002
764
US
I need to do a search/replace on a large number of files with some pretty straight-forward rules. As an example:
Original File

0110 OPEN (1)"bozo-03"
0115 REM OPEN (1,ERR=go_away)"bozo-03"
0120 OPEN (13)"dodo-04"
0200 FIND (1,KEY="BT07)IOL=8105

Desired Results
0110 OPEN (BOZO03_DEV)"bozo-03"
0115 REM OPEN (BOZO03_DEV,ERR=go_away)"bozo-03"
0120 OPEN (13)"dodo-04"
0200 FIND (BOZO03_DEV,KEY="BT07)IOL=8105

The channel number ("1" in this example) will vary. I can run the script multiple times to accomodate this. But, in general this is what I need:
Look for the OPEN (1)"bozo-03" or the alternate of OPEN (1,ERR=something)"bozo-03".
Take the filename which is within the quotes, and use that information to create a replacement for the Channel Number. The conversion would strip out the "-", uppercase the alpha characters, and append "_DEV". So
OPEN (1)"bozo-03" becomes
OPEN (BOZO03_DEV)"bozo-03"
and
OPEN (1,ERR=something)"ivmast-07" would become
OPEN (IVMAST07_DEV,ERR=something)"ivmast_07".

I have this so far to help find the lines which need to be changed:

Code:
file=$1
n_chan=$2
case $n_chan in
        1)
        dfile=`cat $file|egrep '(OPEN \(1\)|OPEN \(1,)'|awk -F \) '{print $2}'`
        ;;
        2)
        dfile=`cat $file|egrep '(OPEN \(2\)|OPEN \(2,)'|awk -F \) '{print $2}'`
        ;;
        *)
        echo "Nope"
        ;;
esac

echo "Found: $dfile"

Please ignore the "needless use of Cat" infraction.
This seems to find the strings I want to replace, but I need help using the "substr" option in awk to create the replacement string.
Ideally, I'd like to run through the list of files once for each different channel number and save the results to a different directory, where I could run some manual comparisons to verify the changes.

Any help is appreciated.
 
replace this:
print $2
by this:
r=toupper($2);gsub(/"/,"",r);gsub(/-/,"",r);print r"_DEV"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
What about this ?
dfile=`awk -F')' '/OPEN \('$n_chan'(,|\))/{r=toupper($2);gsub(/"/,"",r);gsub(/-/,"",r);print r"_DEV"}' $file`

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
What about this ?
Code:
file=$1
n_chan=$2
awk -F')' '/OPEN \('$n_chan'(,|\))/{
  r=toupper($2);gsub(/"/,"",r);gsub(/-/,"",r)
  sub(/ \('$n_chan'/," ("r"_DEV")
}1' $file > $file.new

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks for the quick replies!
Before I could try the first two ideas, you had already furnished the third. In my quick test, it appears to work perfectly.
Once that new string has been created (i.e. BOZO03_DEV), I need to look further thoughout the file to replace any other instance of " (1" with " (BOZO03_DEV". This is reflected in my original post on the line starting with 0200.
I'll give this a try later tonight when I have more time.

Thanks again!
 
OOps, missed the FIND (
Code:
awk -F')' '
/OPEN \('$n_chan'(,|\))/{
  r=toupper($2);gsub(/"/,"",r);gsub(/-/,"",r)
}
{ sub(/ \('$n_chan'/," ("r"_DEV");print }
' $file > $file.new

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
The new version produced exactly the same output. It didn't affect a change on the FIND command. Don't I need to change the awk PatternMatch?
 
Input file:
0110 OPEN (1)"bozo-03"
0115 REM OPEN (1,ERR=go_away)"bozo-03"
0120 OPEN (13)"dodo-04"
0200 FIND (1,KEY="BT07)IOL=8105

script:
file=input
n_chan=1
awk -F')' '
/OPEN \('$n_chan'(,|\))/{
r=toupper($2);gsub(/"/,"",r);gsub(/-/,"",r)
}
/ \('$n_chan'(,|\))/{
sub(/ \('$n_chan'/," ("r"_DEV")
}1' $file > $file.new

Output file:
0110 OPEN (BOZO03_DEV)"bozo-03"
0115 REM OPEN (BOZO03_DEV,ERR=go_away)"bozo-03"
0120 OPEN (13)"dodo-04"
0200 FIND (BOZO03_DEV,KEY="BT07)IOL=8105

Notes:
The FIND (1, is handled
The OPEN (13) is preserved

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top