Splitting a file based on record sin another file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Splitting a file based on record sin another file
# 1  
Old 05-12-2008
Splitting a file based on record sin another file

All,

We receive a file with a large no of records (records can vary) and we have to split it into two files based on another file. e.g.

File1:

UHDR 2008112
"25187","00000022","00",21-APR-1991,"" ,"D",-000000519,+0000000000,"C", ,+000000000,+000000000,000000000,"2","" ,21-APR-1991
"8Y3H4","0000004H","00",16-APR-1992,"" ,"H",-001621119,+0000000000,"C", ,+000000000,+000000000,000000000,"2","" ,21-APR-1991
"95Y8U","02100971","00",03-MAR-1991,"" ,"H",-000004499,+0000000000,"" , ,+000000000,+000000000,000000000,"2","US",21-APR-1991
"24567","02100973","00",26-SEP-1991,"" ,"H",-000000362,+0000000000,"" , ,+000000000,+000000000,000000000,"2","US",21-APR-1991
--
--
--
UTRL 00144700


File2:
2518720080512
2456720080512
1256720080512
8WE7820080512
8Y3H020080512
8Y3H220080512
8Y3H420080512
8Y3H620080512
-
--
--
--

If the first 5 characters of file 2 matched with the chars 2-6 in file1, it should separate those records and put them into another file and rest of the records should be copied into a second file.

I tried cut command but as the file1 is quite large, it was taking a lot of time to put the values into a variable and then compare it.

Is there a way which can do the above task quite fast.

Please help as it is needed urgently.

Thanks in anticipation.
# 2  
Old 05-13-2008
If your grep can read patterns from a file (like GNU grep), try something like this.

Code:
cut -c1-5 file2 >patterns
grep -f patterns file1 >matches
grep -f patterns -v file1 >nonmatches

There is some room for improvement, relating to making the patterns always match at beginning of line, etc. You could replace the first line with something like

Code:
sed 's/^\(.....\).*/^"\1",/' file2 >patterns

This adds the double quotes around the search string and adds a comma behind it and the special character "^" before it, which means match only at beginning of line.

Last edited by era; 05-13-2008 at 04:28 AM.. Reason: Explain revised patterns
# 3  
Old 05-13-2008
With awk:

Code:
awk 'NR==FNR{a[substr($0,1,5)];next}
NF<3{next}
substr($1,2,5) in a {print > "file1";next}
{print > "file2"}' file2 file1

Regards
# 4  
Old 05-13-2008
Thanks Franklin and era.

Franklin,
I fpossible, can you explain wha tthe cod eis doing. I'm an awk novice and this will help me in interpreting things better.
Also, what do I do if I don't want to modify the original files.

Thanks again.
# 5  
Old 05-13-2008
Code:
awk 'NR==FNR{a[substr($0,1,5)];next}

Gets first 5 characters of the first file into array.

Code:
 NF<3{next}

Ignore lines with less then 3 fields (the first and last line)

Code:
 substr($1,2,5) in a {print > "file1";next}

If 5 characters after quotes in array print to file1..

Code:
 {print > "file2"}

... else print to file2


The original files should not change.

Regards

Last edited by Franklin52; 05-13-2008 at 02:03 PM.. Reason: linguistic correction
# 6  
Old 05-13-2008
Franklin,

You rock.
But when I use this:

Code:
awk 'NR==FNR{a[substr($0,1,5)];next}
NF<3{next}
substr($1,2,5) in a {print > "matched.txt";next}
{print > "notmatched.txt"}' file2 file1

I only see matched.txt file but not "notmatched.txt". Am I missing something here?
# 7  
Old 05-13-2008
With the given files it works fine for me.
Are the formats of the files you've posted different from the original files?
The first line of the script use the position 1-5 from the first file as key and it's compared with position 2-6 of the second file. Check the exact positions of your original files. You should be able to fix it now for Smilie.

Regards
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Splitting the file based on two fields - Fixed length file

Hi , I am having a scenario where I need to split the file based on two field values. The file is a fixed length file. ex: AA0998703000000000000190510095350019500010005101980301 K 0998703000000000000190510095351019500020005101480 ... (4 Replies)
Discussion started by: saj
4 Replies

2. UNIX for Beginners Questions & Answers

Splitting a file based on a pattern

Hi All, I am having a problem. I tried to extract the chunk of data and tried to fix I am not able to. Any help please Basically I need to remove the for , values after K, this is how it is now A,, B, C,C, D,D, 12/04/10,12/04/10, K,1,1,1,1,0,3.0, K,1,1,1,2,0,4.0,... (2 Replies)
Discussion started by: arunkumar_mca
2 Replies

3. Shell Programming and Scripting

Keeping record of file 2 based on a reference file 1 in awk

I have 2 input files (tab separated): file1: make_A 1990 foo bar make_B 2010 this that make_C 2004 these those file2: make_X 1970 1995 ref_1:43 ref_2:65 make_A 1970 1995 ref_1:4 ref_2:21 ref_3:18 make_A 1980 2002 ref_1:7 ref_2:7 ref_3:0 ... (2 Replies)
Discussion started by: beca123456
2 Replies

4. UNIX for Dummies Questions & Answers

Extracting data from one file, based on another file (splitting)

Dear All, I have two files but want to extract data from one based on another... can you please help me file 1 David Tom Ellen and file 2 David|0010|testnamez|resultsz David|0004|testnamex|resultsx Tom|0010|testnamez|resultsz Tom|0004|testnamex|resultsx Ellen|0010|testnamez|resultsz... (12 Replies)
Discussion started by: A-V
12 Replies

5. Shell Programming and Scripting

Need help splitting huge single record file

I was given a data file that I need to split into multiple lines/records based on a key word. The problem is that it is 2.5GB or bigger and everything I try in perl or sed causes a Segmentation fault. Can someone give me some other ideas. The data is of the form:... (5 Replies)
Discussion started by: leolson
5 Replies

6. Shell Programming and Scripting

Splitting a file based on context.

I have file as shown below. Would like to split the file based on the context of data. Like, split the content between "---- XXX Info ----" and " ---- YYY Info ----" to a file. When I try using below command, 2nd file contains all the info starting after first "---- YYYY Info ----" instance.... (8 Replies)
Discussion started by: webkid
8 Replies

7. Shell Programming and Scripting

Splitting a file based on two patterns

Hi there, I've an input file as follows: *START 1001 a1 1002 a2 1003 a3 1004 a4 *END *START 1001 b1 1002 b2 1004 b4 *END *START 1001 c1 1004 c4 *END (6 Replies)
Discussion started by: kbirde
6 Replies

8. Shell Programming and Scripting

Splitting the file based on logic

Hello I have a requirement where i need to split the Input fixed width file which contains multiple invoices into multiple files with 2 invoices per file. Each invoice can be identified by its first line's second character which is "H" and sixth character is " " space and the invoice would... (10 Replies)
Discussion started by: dsdev_123
10 Replies

9. Shell Programming and Scripting

Splitting a file based on the records in another file

All, We receive a file with a large no of records (records can vary) and we have to split it into two files based on another file. e.g. File1: UHDR 2008112 "25187","00000022","00",21-APR-1991,"" ,"D",-000000519,+0000000000,"C", ,+000000000,+000000000,000000000,"2","" ... (2 Replies)
Discussion started by: er_ashu
2 Replies

10. Shell Programming and Scripting

splitting a record and adding a record to a file

Hi, I am new to UNIX scripting and woiuld appreicate your help... Input file contains only one (but long) record: aaaaabbbbbcccccddddd..... Desired file: NEW RECORD #new record (hardcoded) added as first record - its length is irrelevant# aaaaa bbbbb ccccc ddddd ... ... ... (1 Reply)
Discussion started by: rsolap
1 Replies
Login or Register to Ask a Question