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

Conditional string substitution - based on first 4 fields 1

Status
Not open for further replies.

arunrr

Programmer
Oct 2, 2009
103
US
Hello,

I have the following input file (example only, actual file has many more lines)...

A1 A2 B2 B1 Q2 Q3 S2
B4 A2 B2 B1 Q2 Q4 S2
B4 A2 B2 B1 Q1 Q3 S1
A1 A2 B2 B1 Q2 Q3 S2
A1 A2 A3 B1 Q2 Q4 S2

I need to first substitute fields 5 & 6, Qn and Qm, with values from the first 4 fields (n and m correspond to the field position).

For example, the first line would become (after substitution)...
A1 A2 B2 B1 A2 B2 S2

Following this, I then need to replace field 7, Sx, with values from fields 5 and 6. Here, if field 7 has a value of 'S1', it would be changed to the value from field 5 and 'S2' would be changed to the value from field 6.

Continuing with the example, the line would further modify to...
A1 A2 B2 B1 A2 B2 B2

Use of awk or sed or any combination thereof or any other method is fine.

Your help is very much appreciated...
Arun
 
This seems to do the trick:

Code:
awk '{
 gsub("Q|S","",$0);
 i=$5                
 j=$6                
 k=4+$7              
 $5=$i                
 $6=$j
 $7=$k
 print
}' /path/to/your/file

HTH,

p5wizard
 
Awesome!! Works like a charm...
Thanks,
AR
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top