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!

getline 2 fields then print match + content of field 2 in first file 1

Status
Not open for further replies.

madasafish

Technical User
Jul 18, 2006
78
TH
Please advise on how to get nawk to print out the contents of the getline field:

Many Thanks,
Madasafish

rangefile:
hello world

myfile:
goodbye world
hello sir

Wanted result
hello sir|world

Code:
BEGIN {
while (getline < rangefile) {
catno[$1]=1;catno[$2]=$2
}
}
{
if (catno[$1])
{
#print $0 > "foundit"
print $0,catno[$2]"|"
}
}' myfile

 
What about this ?
nawk 'NR==FNR{a[$1]=$2;next}$1 in a{print $0"|"a[$1]}' rangefile myfile

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PHV.

The code works with the simple example given.
When I try it on large files..I get nothing?

Is there a limitation?

Madasafish
 
ooops....sorry my fault, there was a delimiter to specify

eg: -F

Thanks PHV

 

What small change do I need to make to get this funtionality?

Bad Code: :-(
gawk -F"|" 'NR==FNR {a[$1]=$2;next}
{
if ($1 = a)
{
print $0 a[$1]"|" > "good"
}
else
{
print $0 > "bad"
}
}' rangefile myfile

Madasafish
 
Code:
gawk -F"|" 'NR==FNR {a[$1]=$2;next}
        {
        if ($1 [COLOR=red][b]in[/b][/color] a)
                {
                print $0 a[$1]"|" > "good"
                }
        else
                {
                print $0 > "bad"
                }
        }' rangefile myfile


Jean-Pierre.
 
Why?????

It must be something to do with the -F"|"

I have two files that are very similiar,
like this:
DC000600CN003350647 290 0
DC000600CN003350647 291 0
DC000600CN003350647 292 0
DC000600CN003350647 293 0
DC000600CN003350647 294 0
DC000600CN003350647 295 0
DC000600CN003350647 296 0
DC000600CN003350647 297 0
DC000600CN003350647 298 0
DC000600CN003350647 299 0

For simplicity I will use the following code: Like before (above)

FILE1=File1
FILE2=File2

gawk 'NR==FNR {a[$1]=$2;a[$2]=$3;next}
{
if ($1 in a && a[$1]=$2)
{
print $0,a[$2]
}
}' $FILE1 $FILE2

The result of this is:
0C000600CN003350647 297 1
0C000600CN003350647 296 0
0C000600CN003350647 295 0
0C000600CN003350647 294 0
0C000600CN003350647 293 0
0C000600CN003350647 292 0
0C000600CN003350647 291 0
0C000600CN003350647 290 0
DC000600CN003350647 288 0

I would of expected and want the output to look like this.

DC000600CN003350647 297 1 0
DC000600CN003350647 296 0 0
DC000600CN003350647 295 0 0
.....
.....
etc

it looks like it wrapping a[$2] to the beginning of the line?

Bizarley!!
This code works with Cygwin bash but will not do the same in
Linux bash
Any help appreciated

Madasafish



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top