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!

Modify the 1st column based on the 2nd column

Status
Not open for further replies.

kobewins

Programmer
Dec 1, 2005
57
US
Hi,

I need some ideas and help for creating a unix script. I have the following input file. I need the output like this:

if the first column is 'R', print 'R';
if the first column is 'A', then check the 2nd column
if 2nd col contains 'FA0', print
'R' 'FA01FBxx'
else, print
'A'

I was trying to use awk, like
cat input_file | awk ' $2 ~ /FA0/ {print "R" } '
or
cat input_file | awk '/FA0/{print "R" }'
Both of the scripts give me six 'R's.

How can I put the logic if Col 2 ($2) doesn't contains 'FA0'? Can I do something like this:
cat input_file | awk ' if($2 ~ /FA0/) {print "R" } \
else {print "A" }'
?

Then how I can get FA01FBxx from $2?

Thank you.
David

1. Input file

R 20051230^]^^^\AM21^\ANR^\F318421^\FA01^\FB52^\FQREFERENCE
R 20051230^]^^^\AM21^\ANR^\F318451^\FA01^\FB52^\FQREFERENCE
A 20060105^^^\AM21^\ANR^\F392821^\FA01^\FB70^^^\AM22^\EM1^\D20001111^M^M
A 20060105^^^\AM21^\ANR^\F392941^\FA01^\FB70^^^\AM22^\EM1^\D20001111^M^M
A 20060105^^^\AM25^\C1470^^^\AM23^\F50000010{^\F610{^\AV2^\F90000000{^\FM4^M^M
A 20060105^^^\AM25^\C14702MUITCTC65...FA01^\FB70^^^\AM22^\EM1^\D20001111^M^M
A 20060105^^^\AM21^\ANR^\F393271^\FA01^\FB70^^^\AM22^\EM1^\D20001111^M^M

2. Output:

R FA01FB52
R FA01FB52
R FA01FB70
R FA01FB70
A
R FA01FB70
R FA01FB70
 
nawk -f kob.awk input_file

nawk.awk:
Code:
BEGIN {
  PATsearch="FA0[0-9]*"
}


function getTrailer (field,   prefix, t, trailer) {
  if ( match(field, PATsearch) ) {
     prefix=substr(field, RSTART, RLENGTH)
     t=substr(field, RSTART+RLENGTH)
     if (match(t, "[A-Za-z0-9]+"))
        trailer=substr(t, RSTART, RLENGTH)
  }
  return ( prefix trailer )
}

$1 == "R" {
   print $1, getTrailer($2)
   next
}

$1 == "A" {
   a=getTrailer($2)
   printf("%s\n", (length(a)) ? "R" OFS a : $1)
}

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thank you very very much, vlad, for your solution. It works perfectly.

However, there is certain stuff I can't understand. I was just wondering you could give me some education.

Function getTrailer is defined as:
function getTrailer (field, prefix, t, trailer)

but it is called as: a=getTrailer($2).

I can guess prefix() is the reason, but how RSTART, RLENGTH work with it? Can you please explain how the getTrailer() works?

How the match() works?

Sorry for the silly questions.

Thanks
David
 
kobewins said:
Thank you very very much, vlad, for your solution. It works perfectly.

However, there is certain stuff I can't understand. I was just wondering you could give me some education.

Function getTrailer is defined as:
function getTrailer (field, prefix, t, trailer)

but it is called as: a=getTrailer($2).

this is the awk way to declare 'local' variables to a function. The variables 'prefix, t, trailer' are considered local to a function and are initialized when the function is called.

kobewins said:
I can guess prefix() is the reason, but how RSTART, RLENGTH work with it? Can you please explain how the getTrailer() works?

How the match() works?
here's the quote from 'man nawk'
man nawk said:
match(s,ere)
Return the position, in characters, numbering from
1, in string s where the extended regular expres-
sion ere occurs, or zero if it does not occur at
all. RSTART will be set to the starting position
(which is the same as the returned value), zero if
no match is found; RLENGTH will be set to the
length of the matched string, -1 if no match is
found.
kobewins said:
Sorry for the silly questions.

Thanks
David

Try reading 'man nawk' first. If you have more questions pls don't hesitate to repost again!

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top