print records where an ID appears more than once


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers print records where an ID appears more than once
# 1  
Old 01-11-2011
print records where an ID appears more than once

Hi everyone! I have something like the following records in a file:

ID: 1
Name: A
Age: 23

ID: 2
Name: B

ID: 1
Activity: Climbing

ID: 3
Name: C

ID: 3
Activity: Skating

I want to capture the records where the field Age exists AND the records that have the same ID as the one where the Age existed. So in the example above, only record with ID:1 is printed to the output file:
ID: 1
Name: A
Age: 23

ID: 1
Activity: Climbing

ID:2 is ignored because it doesn't have neither the AGE field nor any other ID:2 record follows; so as for ID:3, because the field AGE is missing.

I hope I have explained this well and that it is possible to do this with UNIX commands.

Thanks in advance, Atrisa
# 2  
Old 01-11-2011
I suppose in a shell you could put all fields in arrays by id, and then walk the age arrray by id printing all for good ages. Your problem is a bit of a sort and join filtering with sparse matrix.
# 3  
Old 01-11-2011
What software produced this output? It is often easier to process the data in the original programming language (whatever that is?) with direct access to the database rather than trying to use unix Shell to process what amounts to print layout.

Ps. Please post data samples in Code Tags.
# 4  
Old 01-11-2011
Code:
 awk -vRS="" -vFS="\n"  '/Age/{re=$1} $0~re{print }'  file

This User Gave Thanks to JavaHater For This Post:
# 5  
Old 01-12-2011
Thanks everyone. JavaHater's command did the trick. thanks again.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print Terminal Output Exactly how it Appears in the Terminal to a New Text File

Hello All, I have a text file containing output from a command that contains lots of escape/control characters that when viewed using vi or view, looks like jibberish. But when viewed using the cat command the output is formatted properly. Is there any way to take the output from the cat... (7 Replies)
Discussion started by: mrm5102
7 Replies

2. Shell Programming and Scripting

awk print matching records and occurences of each record

Hi all , I have two files : dblp.xml with dblp records and itu1.txt with faculty members records. I need to find out how many dblp records are related to the faculty members. More specific: I need to find out which names from itu1.txt are a match in dblp. xml file , print them and show how many... (4 Replies)
Discussion started by: iori
4 Replies

3. Shell Programming and Scripting

Shell script to print date and no.of records

hi all, i want script to print the output in the following format filename yyyy/mm/dd count where count= no.of records in the file Thanks in advance hemanthsaikumar (11 Replies)
Discussion started by: hemanthsaikumar
11 Replies

4. Shell Programming and Scripting

Print unique records in 2 columns using awk

Is it possible to print the records that has only 1 value in 2nd column. Ex: input awex1 1 awex1 2 awex1 3 assww 1 ader34 1 ader34 2 output assww 1 (5 Replies)
Discussion started by: quincyjones
5 Replies

5. Shell Programming and Scripting

Print other records along with condition too

Hi, I have a text file with more than 100000 records. I used the following command awk '{if ($3<$2) print $1"\t"$3"\t"$2"\t"$4"\t"$5}' input.txt > output.txt But, this one is printing which satisfies the condition, which are only 1000. I would like to get all the 100000 records in... (4 Replies)
Discussion started by: jacobs.smith
4 Replies

6. Linux

help to print all the contents under chosen records in perl

Hi, I have a file in which I have to separate genes from phenotype data. I have written a program as given below which prints just the gene records. 1. But I want to print all the data under each specified record to the output file. So the file has all the data like for every record. An example... (2 Replies)
Discussion started by: kaav06
2 Replies

7. Shell Programming and Scripting

awk print only select records from file2

Print only records from file 2 that do not match file 1 based on criteria of comparing column 1 and column 6 Was trying to play around with following code I found on other threads but not too successful Code: awk 'NR==FNR{p=$1;$1=x;A=$0;next}{$2=$2(A?A:",,,")}1' FS=~ OFS=~ file1 FS="*"... (11 Replies)
Discussion started by: sigh2010
11 Replies

8. Shell Programming and Scripting

AWK, print no of records after pattern match.

Hi ALL :). i have a file, cat 3 + dog 5 + rat 6 - i want to print no of record having pattern "+". thanks in advance :confused:. (2 Replies)
Discussion started by: admax
2 Replies

9. Shell Programming and Scripting

print records before and after the pattern

Hi All, I want to write a sh shell script to read input report and search for a specefic pattern in file and print 3 record above and 2 record below the line in which pattern found. My file contains different- different pattern repeatedly, same pattern set of records should go into one file... (9 Replies)
Discussion started by: singhald
9 Replies

10. UNIX for Dummies Questions & Answers

AWK ??-print for fields within records in a file

Hello all, Would appreciate if someone can help me out on the following requirement. INPUT FILE: -------------------------- TPS REPORT abc def ghi jkl mon pqr stu vrs lll END OF TPS REPORT TPS REPORT field1 field2 field3 field4 field5 field6 (8 Replies)
Discussion started by: hyennah
8 Replies
Login or Register to Ask a Question