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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Substitute values based on conditions 1

Status
Not open for further replies.

lbzh

Programmer
Aug 7, 2004
25
US
I would like to write a shell script to screen on some conditions for a given line and if valid perform 2 substitutions on that line otherwise the line shall remain as-is.

Here is the file:
1234NYABC1LMA
2334NJABC0TR3
4234NJABC1LMA
5234PAABC1LMA
8234SCABC1ZBC

If the state (columns 5 and 6) is either NY or NJ
and the Validity Indicator (column 10) is 1
then replace the state (columns 5 and 6) with NE
and replace column 13 with a Y, otherwise the line remains the same:

the output would be:

1234NEABC1LMY
2334NJABC0TR3
4234NEABC1LMY
5234NEABC1LMY
8234SCABC1ZBC

Any tips would be greatly appreciated.

Thanks
 
A starting point:
awk '{
s=substr($0,5,2)
if((s=="NY" || s=="NJ") && substr($0,10,1)=="1")
print substr($0,1,4)"NE"substr($0,7,6)"Y"substr($0,14)
else
print
}' /path/to/input > output

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

Part and Inventory Search

Sponsor

Back
Top