Pick a line in file 2 basing on array in file1


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pick a line in file 2 basing on array in file1
# 1  
Old 09-24-2013
Pick a line in file 2 basing on array in file1

Dear friends,

I have two files. One with all IDs(in a single field) . And another with data(of which say field 5 is ID). I want to create an array of IDs using first file and
while reading second file if the ID appears in the array I need to print $0 else skip.

After a long gap I am working again in awk.

My code is:
Code:
FILENAME == "ID.TXT" { VALIDID[$1]=$1}
FILENAME == "DATA.TXT" { ID=$5;
IF (ID IN VALIDID) PRINT }

Your help is highly appreciated.

PND
Moderator's Comments:
Mod Comment please use code tags for your code and data next time!

Last edited by vbe; 09-24-2013 at 09:56 AM..
# 2  
Old 09-24-2013
try:
Code:
awk -f 1.awk ID.TXT DATA.TXT

where 1.awk:
Code:
FILENAME == "ID.TXT" { VALIDID[$1]=$1}
FILENAME == "DATA.TXT" { ID=$5;
if (ID in VALIDID) print }

# 3  
Old 09-24-2013
not sure why you want to use awk but you could simply use join.
# 4  
Old 09-24-2013
... or grep -f ID.TXT DATA.TXT ?
# 5  
Old 09-25-2013
Than you. This worked. But one thing I wanted to know,
my code looks fine, but returns nothing. If someone can help me
understand how array is handled here, it will help me with other such
situations as well.
# 6  
Old 09-25-2013
You shouldn't be using caps in the awk script.

--ahamed
# 7  
Old 09-25-2013
Try
Code:
FILENAME == "ID.TXT" { VALIDID[$1]=$1}
FILENAME == "DATA.TXT" && ($5 in VALIDID)

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Deleting file basing on the timestamp substring in the file name

Hello, I have in my backup folder, files with names convention like this : randomFileNames_13-02-2014_23h13m09+1392333189 randomFileNames_14-02-2014_02h13m09+1392343989 randomFileNames_14-02-2014_04h13m09+1392351189 etc.... Base on timestamp at end of the filename, I would to delete all the... (7 Replies)
Discussion started by: thuyetti
7 Replies

2. Shell Programming and Scripting

Using regex's from file1, print line and line after matches in file2

Good day, I have a list of regular expressions in file1. For each match in file2, print the containing line and the line after. file1: file2: Output: I can match a regex and print the line and line after awk '{lines = $0} /Macrosiphum_rosae/ {print lines ; print lines } ' ... (1 Reply)
Discussion started by: pathunkathunk
1 Replies

3. Shell Programming and Scripting

Pick a card from an array in bash

Hello, I am trying to use $RANDOM to pick cards from an array in bash. I created the script below to give me a selection everytime I run it, but the output is always like this: 2 3 4 5 6 7 8 9 10 Jack Queen King Ace of Clubs Diamonds Hearts Spades. Can you please help me? I expected it to... (2 Replies)
Discussion started by: Pouchie1
2 Replies

4. Shell Programming and Scripting

Replacing line 'i' of file1 with line 'j' of file 2

Hi All, As mentioned in the title I have two text files and I would like to replace line number 5 of file #1 with line number 4 of file #2 e.g. file 1 wqwert 4.4464002 3 319 286 369 46.320002 56.150002 45.100002 1 1 1 0.723 (12 Replies)
Discussion started by: f_o_555
12 Replies

5. UNIX Desktop Questions & Answers

awk to pick out more than one line

This really is a dummy question but I'm stuck and out of time... I have a large file and out of it I only want to pick out lines starting with either "Pressure" or "N". I still need these lines to be in their original order. example of text Pressure 3 N 2 N 3 bla bla bla bla Pressure 4... (3 Replies)
Discussion started by: jenjen_mt
3 Replies

6. Shell Programming and Scripting

Split a single file into several others basing on the last column

Hi folks, Happy new year. I have a file 'filename' that i wd like to split basing on the contents in the last column. The 'filename' content looks like 256772744788,9,11 256772744805,9,11 256772744792,9,11 256775543055,10,12 256782625357,9,12 256772368953,10,13 256772627735,10,13... (3 Replies)
Discussion started by: jerkesler
3 Replies

7. UNIX for Advanced & Expert Users

how do you parse 1 line at a time of file1 ie. line(n) each line into new file

File 1 <html>ta da....unique file name I want to give file=>343...</html> <html>da ta 234 </html> <html>pa da 542 </html> and so on... File 2 343 234 542 and so on, each line in File 1 one also corresponds with each line in File 2 I have tried several grep, sed, while .. read, do,... (4 Replies)
Discussion started by: web_developer
4 Replies

8. Shell Programming and Scripting

Compare multiple fields in file1 to file2 and print line and next line

Hello, I have two files that I need to compare and print out the line from file2 that has the first 6 fields matching the first 6 fields in file1. Complicating this are the following restrictions 1. file1 is only a few thousand lines at most and file2 is greater than 2 million 2. I need to... (7 Replies)
Discussion started by: gillesc_mac
7 Replies

9. Shell Programming and Scripting

cat file1 read line-per-line then grep -A 15 lines down in fileb

STEP 1 # Set variable FILE=/tmp/mainfile SEARCHFILE =/tmp/searchfile # THIS IS THE MAIN FILE. cat /tmp/mainfile Interface Ethernet0/0 "outside", is up, line protocol is up Hardware is i82546GB rev03, BW 100 Mbps Full-Duplex(Full-duplex), 100 Mbps(100 Mbps) MAC address... (6 Replies)
Discussion started by: irongeekio
6 Replies

10. Shell Programming and Scripting

Split a binary file into 2 basing on 2 delemiter string

Hi all, I have a binary file (orig.dat) and two special delimiter strings 'AAA' and 'BBB'. My binary file's content is as follow: <Data1.1>AAA<Data1.2>BBB <Data2.1>AAA<Data2.2>BBB ... <DataN.1>AAA<DataN.2>BBB DataX.Y might have any length, and contains any kind of special/printable... (1 Reply)
Discussion started by: Averell
1 Replies
Login or Register to Ask a Question