Computing.Net > Forums > Unix > MAtching files agaist the key

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

MAtching files agaist the key

Reply to Message Icon

Name: bengalliboy
Date: August 18, 2004 at 06:43:02 Pacific
OS: UNIX
CPU/Ram: 450
Comment:

I got a file that contains the key only. Records are unique.

I want to take that key and go against multiple files and extract record from them. Note, the files that I am trying to match may contain my key in a fixed position. Say, for file A the key is from 12 to 20 and for file B it is in 0 to 9.
I know I can use grep agains the key file for matching some thing like:
fgrep -f record_key data_out >data_updated

but how do I go against maching those particular fields. Also is there an easy way to go against both the files same time ?
Many thnaks again!!!

Report Offensive Follow Up For Removal




Sponsored Link
Ads by Google

Response Number 1
Name: Jim Boothe
Date: August 18, 2004 at 09:04:02 Pacific
Reply:

This solution loads your keys into an array, then processes two files against it.  It checks which FILENAME is being processed to locate the key.

awk 'BEGIN \
{while ((getline < "mykeys") > 0)
    keyfile[$1] = 1
}
{if (FILENAME == "fileA")
    key=substr($0,11,9)
 else
    key=substr($0,1,10)
 if (key in keyfile)
    print
}' fileA fileB


0

Response Number 2
Name: bengalliboy
Date: August 18, 2004 at 12:26:27 Pacific
Reply:

Thanks a lot Jim! This works. Now that I got my key and matches, Instead of printing them, I want to redirect to files in different dir with the same name some thing like:
/new_dir/fileA
/new_dir/fileB

Note: Results only should end up on these files. So what do I change instead of "Print" in the above AWK.
Again Thanks a lot!!!


0

Response Number 3
Name: Jim Boothe
Date: August 18, 2004 at 12:57:53 Pacific
Reply:

This revised solution outputs to separate files based on the current input file.  You do not need to print with append (print >>) because awk keeps all output files opened until either an explicit close or end or program.  Use the append only if you actually want to append to existing files.

awk 'BEGIN \
{while ((getline < "mykeys") > 0)
    keyfile[$1] = 1
}
{if (FILENAME == "fileA")
    {key=substr($0,11,9)
     outfile="/new_dir/fileA"}
 else
    {key=substr($0,1,10)
     outfile="/new_dir/fileB"}
 if (key in keyfile)
    print > outfile
}' fileA fileB


0

Response Number 4
Name: bengalliboy
Date: August 18, 2004 at 13:01:23 Pacific
Reply:

Great ! Just learned some thing regarding AWK!
Many Thanks!!!!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: MAtching files agaist the key

getting 9 char from a file... www.computing.net/answers/unix/getting-9-char-from-a-file/6433.html

cancel the enter key in a text www.computing.net/answers/unix/cancel-the-enter-key-in-a-text-/7344.html

Send me the codes 22/02/2002 www.computing.net/answers/unix/send-me-the-codes-22022002/2898.html