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!

To use AWK to find certain two fields from two files and create a thir

Status
Not open for further replies.

bholav

Technical User
Jun 13, 2002
15
US
I need to compare ascii characters in fields from two different files and create a third file which has a result of the comparison in the same field.
 

Hi awktonished!

This is an example.

1st input file:
Code:
a 1
b 2
c 2

2nd input file:
Code:
a 22
c 2
c 11

awk program compares 1st column of files:
Code:
BEGIN {
    fileNr = 1
    cnt = 0
}

# saves the filename of the 1st file
{
    if (fileName == "")
        fileName = FILENAME
}


# reads 1st file and puts $1 into array
fileNr == 1 {
    cnt ++
    a[cnt] = $1

    if (fileName != FILENAME) {
        fileNr = 2
        cnt = 0
    }
}

# reads 2nd file, compares value from array with $1 of 2nd file, and
# prints result
fileNr == 2 {
    cnt ++
    if ($1 == a[cnt])
        print "equal"
    else
        print "different"
}


Output:
Code:
equal
different
equal

You can redirect output to 3rd file:
Code:
awk -f program.awk file1.txt file2.txt > file3.txt

I hope this helps.


KP.
 
Hello , thanks for respoding..
when I run this on two files that has similar lines ( fields separated by tab) like:

......
111111
......

I don't get a :

equal
equal
eqaul

instead the third file is blank....

Also,

a.how do a conert a file that has a ascii string like:

297890012
in to different fields like :
2 9 7 8 9 0 0 1 2
so that they can be read as differnt fields


b. how to extract a field from within a file and rename the file as the field's content ?

for example if the file name is abcdf.stdf and a field within that file reads lot12345.f1x , change the file name to lot12345.f1x

thanks a lot,

 
It works (pls ignore my comment above), the file names cannot be similar. Thanks, how do I set a loop to do the same for 35 fields?Thank you.
 
c.how to limit the comparison to certain ASCII characters and ignore comparison if a certain character say , a "." is encountered .
Thanks,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top