Selecting records from file on criteria.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Selecting records from file on criteria.
# 1  
Old 12-16-2004
Question Selecting records from file on criteria.

Can I have 2 files as in input to the awk command?

Situation is somewhat below,

File A contains number & value delimited by a space.
File B contains number as a part of a line. I am not supposed
to retrieve more than 1 number from a line.

If number from file B matches with number from file A, then I will
retrieve value part from file A. I will store this found,
number - value pair in file C.

Would it be possible with single awk command? Or is there any suggestions to implement this?
# 2  
Old 12-16-2004
Use paste file file2 | awk

paste creates one big line from the 'parallel' lines in the input
files

example:
Code:
file1:
dog
cat 
chicken

file2:
big
bigger


paste file1 file2
output:
dog big
cat bigger
chicken

# 3  
Old 12-16-2004
I do some thing like this ...

>file3
exec < file2
while read line
do
grep "$line" file1 >> file3
done

sort -u file3
# 4  
Old 12-17-2004
Bhargav

if I use exec my telnet window disappears.
None of your solutions suggested work for me.

I have to read file A, read a number
Read file B search for this number & extract description beyond it.

File A

xxxx1111yyyyy
aaaa2222bbbbb
cccc3333ddddd

File B
1111 desc1
2222 desc2

I know, awk deals with only 1 file.

Any suggestions?
# 5  
Old 12-17-2004
It seems to me if you gave us all of the requirements up front you would get a better answer.

What is your exact output supposed to be? just the description?
Or the common number field followed by the description?

try
Code:
man join

for your edification.

Based on your requirements so far:
Code:
awk ' { print substr($0,4,4) } ' file1 | sort -u  tmp.sed
grep -f tmp.sed file2 | awk ' {  print $2 } '

This prints descriptions for each record in file1.
# 6  
Old 12-17-2004
Computer

Jim
Thanks, Your solution helps.

Guys/Gals please suggest me good links where I can start with basics of shell scripts including awks.
# 7  
Old 12-17-2004
Using awk ... u were originally looking for .....


awk -v s="file2" '{
while(getline a[x++] <s ) ;
for(x in a)
{
if($0 ~ substr(a[x++],5,4))
{
print $0 ;
}
}

}' file1
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Insert content from file 1 to file 2 in specific criteria meet

Hi , I'm looking for some code that can copy and paste form file1 to file2 with 2 criterial meet. file1: test "sp-j1" test "sp-j2" test "sp-j3" test "sp-j4" file2: sub Pre_Shorts1 (Status_Code, Message$) global Status !if Message$ <> "" then print... (3 Replies)
Discussion started by: kttan
3 Replies

2. Shell Programming and Scripting

Awk/sed/cut to filter out records from a file based on criteria

I have two files and would need to filter out records based on certain criteria, these column are of variable lengths, but the lengths are uniform throughout all the records of the file. I have shown a sample of three records below. Line 1-9 is the item number "0227546_1" in the case of the first... (15 Replies)
Discussion started by: MIA651
15 Replies

3. Shell Programming and Scripting

Separate records of a file on 2 types of records

Hi I am new to shell programming in unix Please if I can provide help. I have a file structure of a header record and "N" detail records. The header record will be the total number of detail records I need to split the file in 2: One for the header Another for all detail records Could... (1 Reply)
Discussion started by: jamcogar
1 Replies

4. Shell Programming and Scripting

Input file is uncolored; I want the output file to be colored on criteria

Hello, I have the following input file: auditing account: 3DTP (3dtp) ERROR: S3 bucket "aws-origin-test1.3dstage.com" has policy statement with public grant: {"Sid":"PublicReadGetObject","Effect":"Allow","Principal":{"AWS":"*"},"Action":,"Resource":} auditing region: eu-west-1 auditing... (5 Replies)
Discussion started by: ramky79
5 Replies

5. Shell Programming and Scripting

Extract error records based on specific criteria from Unix file

Hi, I look for a awk one liner for below issue. input file ABC 1234 abc 12345 ABC 4567 678 XYZ xyz ght 678 ABC 787 yyuu ABC 789 7890 777 zxr hyip hyu mno uii 678 776 ABC ty7 888 All lines should be started with ABC as first field. If a record has another value for 1st... (7 Replies)
Discussion started by: ratheesh2011
7 Replies

6. Shell Programming and Scripting

Deleting duplicate records from file 1 if records from file 2 match

I have 2 files "File 1" is delimited by ";" and "File 2" is delimited by "|". File 1 below (3 record shown): Doc1;03/01/2012;New York;6 Main Street;Mr. Smith 1;Mr. Jones Doc2;03/01/2012;Syracuse;876 Broadway;John Davis;Barbara Lull Doc3;03/01/2012;Buffalo;779 Old Windy Road;Charles... (2 Replies)
Discussion started by: vestport
2 Replies

7. UNIX for Dummies Questions & Answers

Grep specific records from a file of records that are separated by an empty line

Hi everyone. I am a newbie to Linux stuff. I have this kind of problem which couldn't solve alone. I have a text file with records separated by empty lines like this: ID: 20 Name: X Age: 19 ID: 21 Name: Z ID: 22 Email: xxx@yahoo.com Name: Y Age: 19 I want to grep records that... (4 Replies)
Discussion started by: Atrisa
4 Replies

8. Shell Programming and Scripting

Selecting rows with a specific criteria

Hi, I want a UNIX command that can filter out rows with certain criteria. The file is tab deliminated. Row one is just a value. Basically what I want to do is select based on the name and character at the end (o). So lets lets say i want a row that has WashU and (o) then it would print... (2 Replies)
Discussion started by: phil_heath
2 Replies

9. Shell Programming and Scripting

Count No of Records in File without counting Header and Trailer Records

I have a flat file and need to count no of records in the file less the header and the trailer record. I would appreciate any and all asistance Thanks Hadi Lalani (2 Replies)
Discussion started by: guiguy
2 Replies

10. UNIX for Dummies Questions & Answers

Select records based on search criteria on first column

Hi All, I need to select only those records having a non zero record in the first column of a comma delimited file. Suppose my input file is having data like: "0","01/08/2005 07:11:15",1,1,"Created",,"01/08/2005" "0","01/08/2005 07:12:40",1,1,"Created",,"01/08/2005"... (2 Replies)
Discussion started by: shashi_kiran_v
2 Replies
Login or Register to Ask a Question