Preserve lines of data file that appear in another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Preserve lines of data file that appear in another file
# 1  
Old 03-05-2012
Preserve lines of data file that appear in another file

Hello,
I have two data files. I need to cull each line of file1.dat if column #1 appears in file2dat. Example:

file1.dat

Code:
1234 5
2345 3
3456 67
4567 21
5678 9
6789 22
7890 54

file2.dat

Code:
zzzz,'3456',xxx
zzzz,'2345',xxx
zzzz,'6789',xxx

Desired output:

Code:
3456 67
2345 3
6789 22

Thanks!
# 2  
Old 03-05-2012
Hi palex,

One way using perl:
Code:
$ cat file1.dat
1234 5
2345 3
3456 67
4567 21
5678 9
6789 22
7890 54
$ cat file2.dat
zzzz,'3456',xxx
zzzz,'2345',xxx
zzzz,'6789',xxx
$ cat script.pl
use warnings;
use strict;

die qq[Usage: perl $0 <file1> <file2>\n] unless @ARGV == 2;

open my $fh1, qq[<], shift @ARGV or die;
open my $fh2, qq[<], shift @ARGV or die;

my %num;

while ( <$fh2> ) {
        chomp;
        my @f = split /,/, $_;
        next unless @f >= 2;
        $f[1] =~ tr/'//d;
        $num{ $f[1] } = 1;
}

while ( <$fh1> ) {
        chomp;
        my @f = split;
        next unless @f >= 2;
        if ( exists $num{ $f[0] } ) {
                printf qq[%s\n], $_;
        }
}
$ perl script.pl file1.dat file2.dat 
2345 3
3456 67
6789 22

This User Gave Thanks to birei For This Post:
# 3  
Old 03-05-2012
Here is another approach

Code:
$ cat p1.txt
1234 5
2345 3
3456 67
4567 21
5678 9
6789 22
7890 54

$ cat p2.txt
zzzz,'3456',xxx
zzzz,'2345',xxx
zzzz,'6789',xxx

$ cut -d, -f2 <p2.txt | tr -d "'" >p2a.txt


$ grep -f p2a.txt <p1.txt
2345 3
3456 67
6789 22

This User Gave Thanks to joeyg For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merge data in lines from same file

Need help figuring out how to merge data from a file. I have a large txt file with some data that needs to be merged from separate lines into one line. Doug.G|3/12/2011|817-555-5555|Portland Doug.G|3/12/2011|817-555-5522|Portland Steve.F|1/11/2007|817-555-5111|Portland... (5 Replies)
Discussion started by: cdubu2
5 Replies

2. Shell Programming and Scripting

awk - mixed for and if to select particular lines in a data file

Hi all, I am new to AWK and I am trying to solve a problem that is probably easy for an expert. Suppose I have the following data file input.txt: 20 35 43 20 23 54 20 62 21 20.5 43 12 20.5 33 11 20.5 89 87 21 33 20 21 22 21 21 56 87 I want to select from all lines having the... (4 Replies)
Discussion started by: naska
4 Replies

3. UNIX for Advanced & Expert Users

Extracting specific lines from data file

Hello, Is there a quick awk one-liner for this extraction?: file1 49389 text55 52211 text66 file2 59302 text1 49389 text2 85939 text3 52211 text4 13948 text5 Desired output 49389 text2 52211 text4 Thanks!! (5 Replies)
Discussion started by: palex
5 Replies

4. Shell Programming and Scripting

Extracting specific lines of data from a file and related lines of data based on a grep value range?

Hi, I have one file, say file 1, that has data like below where 19900107 is the date, 19900107 12 144 129 0.7380047 19900108 12 168 129 0.3149017 19900109 12 192 129 3.2766666E-02 ... (3 Replies)
Discussion started by: Wynner
3 Replies

5. Shell Programming and Scripting

Deleting shorter lines from data file

Hello, I have the following data file structure: 1234 text 2345 text 3456 text text text 4567 text text text 5678 text text text 6789 text text text I simply want to delete all of the lines that only have one text column (i.e. the first two lines in this case). The output would be:... (1 Reply)
Discussion started by: palex
1 Replies

6. Shell Programming and Scripting

Delete lines line by match data 2 file.

i need to delete the lines is match from file data 1 & data 2 please help? data 1 4825307 4825308 4825248 4825309 4825310 4825311 4825336 data 2 4825248 0100362210 Time4Meal 39.00 41.73 MO & MT MT SMS 4825305 0100367565... (2 Replies)
Discussion started by: ooilinlove
2 Replies

7. UNIX for Dummies Questions & Answers

Delete lines from a file where data is continously appended

Hello , Is there a way to delete lines from a file where data is continously appended to the file. I can use normal vi command ndd to remove n number of lines from the file, as the data is continously appended the line numbers doesnt work. (1 Reply)
Discussion started by: sophos
1 Replies

8. Shell Programming and Scripting

Find lines in text file with certain data in first field

Hi all, Sorry for the title, I was unsure how to word my issue. I'll get right to the issue. In my text file, I need to find all lines with the same data in the first field. Then I need to create a file with the matching lines merged into one. So my original file will look something like... (4 Replies)
Discussion started by: rstev39147
4 Replies

9. UNIX for Advanced & Expert Users

How to print data between 2 lines in a file

i want to print the data between two line numbers in file to another file. which command should i use i tried sed command . i am storing the line numbers in two variables say L1,L2. but $L1 and $L2 are not working in sed command. is there any other command to do this reply soon (5 Replies)
Discussion started by: kamesh83
5 Replies

10. Shell Programming and Scripting

Pulling data and following lines from file

I saw a few posts close to what i want to do, but they didn't look like they would work exactly.. or I need to think out of the box on this. I have a file that I keep server stats in for my own performance analysis. this file has the output from many commands in it (uptime, vmstats, ps, swap... (2 Replies)
Discussion started by: MizzGail
2 Replies
Login or Register to Ask a Question