extract set of matching records


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract set of matching records
# 1  
Old 12-23-2008
extract set of matching records

i have a pipe delimited file with records spread in many lines.
i need to extract those records
1)having X in beginning of that record

2)and having at least one Y in beginning before other record begins

eg:
X|Rec1|
A|Rec1|
Y|Rec1|

X|Rec2|
Y|Rec2|

Z|Rec3|

X|Rec4|
M|Rec4|

X|Rec5|

so, lines Rec1,Rec2 have to be in out.DAT and Rec3,Rec4,Rec5 in err.DAT
thanks.
# 2  
Old 12-23-2008
Hi,

try:

Code:
awk  'BEGIN{FS="\n";RS=""}\
    /X/ && /Y/{printf "%s\n\n",$0 > "good"};\
    !(/X/ && /Y/){printf "%s\n\n",$0 > "bad"}' file

HTH Chris
# 3  
Old 12-23-2008
thanks Chris. this will surely help. But, if different records are not separated by one or more blank lines, how to isolate records?
like then what will i put in RS?

eg:-
X|Rec1|
Y|Rec1|
X|Rec2|
z|Rec3|
# 4  
Old 12-23-2008
Then you'll have to separate them:

Code:
awk -F'[|]|Rec' 'BEGIN{old=1};\
    $3 > old{printf "\n%s\n", $0;old=$3};\
    $3==old{printf "%s\n",$0;old=$3}' file2

and afterwards run the other command.

Tested with:
Code:
X|Rec1|
A|Rec1|
Y|Rec1|
X|Rec2|
Y|Rec2|
Z|Rec3|
X|Rec4|
M|Rec4|
X|Rec5|

HTH Chris
# 5  
Old 12-24-2008
Code:
nawk 'BEGIN{
RS=""
}
/^X/ && /Y/ {
printf("%s\n\n",$0) >> "out.txt"
next
}
{
printf("%s\n\n",$0) >> "err.txt"
}
' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Ufsdump Records in/out not matching

Hello, I'm on a Solaris 10 and doing backup using a tape. The records in/out are way off but with no other errors. Is that normal? What could be wrong? Thanks in advance! Steve --------------------- ufsdump 0f - /dev/dsk/c0d0s0 2>> /var/log/backupOS.log | ssh myhost dd... (1 Reply)
Discussion started by: steve041
1 Replies

2. Shell Programming and Scripting

To get Non matching records for current day

My objective is to get the non matching records of previous day with current day. eg, file1 contains 1 a 2 b and file2 contains: 2 b 3 c then expected output is 3 c¨ another example file 1 contains: 1 a 2 b file 2 contains 1 c 2 b (8 Replies)
Discussion started by: newbie2014
8 Replies

3. Shell Programming and Scripting

awk pattern matching name in records

Hi, I'm very new to these forums. I was wondering if someone could help an AWK beginner with a pattern matching an actor to his appearance in movies, which would be stored as records. Let's say we have a database of 4 movies (each movie a record with name, studio + year, and actor fields with... (2 Replies)
Discussion started by: Jill Ceke
2 Replies

4. Shell Programming and Scripting

Shell script matching similar records

hello all, I have requirement to identify similar records matching about 80% to 90%.I have to black list customers with multiple accounts. The data is in the Oracle Database, but is there any way I can get the data into flat file and compare the strings and fetch similar matching records? ... (2 Replies)
Discussion started by: kashik786
2 Replies

5. Shell Programming and Scripting

Common records after matching on different columns

Hi, I have the following files. cat 1.txt cat 2.txt output.txt The logic is as follows.... (10 Replies)
Discussion started by: jacobs.smith
10 Replies

6. UNIX for Dummies Questions & Answers

Find records with matching patterns

Hi, I need to find records with a search string from a file. Search strings are provided in a file. For eg. search_String.txt file is like below chicago mexico newark sanhose and the file from where the records need to be fetched is given below src_file:... (1 Reply)
Discussion started by: sbhuvana20
1 Replies

7. Shell Programming and Scripting

script to return records which are not matching

file1 abcd efgh ijkl mnop file2 mnop qrst uvwx xyza file3 1234 4567 (8 Replies)
Discussion started by: rajivrsk
8 Replies

8. Shell Programming and Scripting

Removing non matching records

Hi all I have a file with records starting with "Page" has a first column. some of the records have some other junk characters has first column. so pls help me to remove rows which is not having "Page" has a first column. Thanks, Baski (2 Replies)
Discussion started by: baskivs
2 Replies

9. Shell Programming and Scripting

Set of 2 records as one unit

Hi Experts, back at this forum again. Have a tab separated file like this --- ACCNN AMT(E/$) TYPE ID 11233 23.20($) AUTH 339 11233 19.00($) FINAL 339 11234 349.84($) AUTH 42332 11234 ... (12 Replies)
Discussion started by: PG3
12 Replies

10. Shell Programming and Scripting

Based on num of records in file1 need to check records in file2 to set some condns

Hi All, I have two files say file1 and file2. I want to check the number of records in file1 and if its atleast 2 (i.e., 2 or greater than 2 ) then I have to check records in file2 .If records in file2 is atleast 1 (i.e. if its not empty ) i have to set some conditions . Could you pls... (3 Replies)
Discussion started by: mavesum
3 Replies
Login or Register to Ask a Question