1757756 Members
2024 Online
108863 Solutions
New Discussion юеВ

problem with parser

 
SOLVED
Go to solution
amonamon
Regular Advisor

problem with parser

Hello..can anyone give me a clue how to get desired output:

file1:

5555:56:00 789 A
3345:56:18 189 B
1245:56:38 389 A
0045:56:58 689 S
0045:55:51 000 Q
1245:56:44 989 E
2245:56:11 089 E


file2
1245
0045

output:

1245:56:38 389 A
0045:56:58 689 S
0045:55:51 000 Q

I tryed something with comm and join command but I mess myself..

thank you guys
8 REPLIES 8
amonamon
Regular Advisor

Re: problem with parser

sorry output should be:

output:

1245:56:38 389 A
0045:56:58 689 S
0045:55:51 000 Q
1245:56:44 989 E

I forgot last line :)
amonamon
Regular Advisor

Re: problem with parser

again me...

I could do it this way...but takes a lot of time for my big files..

for file in $(< file2); do

grep $file file1 >> myoutout

done

any quicker way?
Kenan Erdey
Honored Contributor
Solution

Re: problem with parser

Hi,

Hope it helps:

awk '{system("grep "$0" file1")}' file2

Kenan.
Computers have lots of memory but no imagination
amonamon
Regular Advisor

Re: problem with parser

kind of stupid...I put solutions for my problem..hahaah :))))

anyways anyone knows better way to optimise this:

join -1 1 -2 1 -t':' -o 2.1,2.2,2.3 file2 file1


thanks everyone.
Dennis Handly
Acclaimed Contributor

Re: problem with parser

>but takes a lot of time for my big files..
>for file in $(< file2); do grep $file file1

You can do it in one shot:
fgrep -f file2 file1

This works if the strings in file2 don't appear in other columns of your data. If they do, you may have to add a "^" (and use grep) at the beginning of each line in file2.
Dennis Handly
Acclaimed Contributor

Re: problem with parser

>I put solutions for my problem. hahaah :))))

Or you're telling us how to not do it. ;-)
amonamon
Regular Advisor

Re: problem with parser

kind of..:)

thank U very very much
Hein van den Heuvel
Honored Contributor

Re: problem with parser

Here is an other way to do it:

$ awk 'BEGIN{while (getline line<"file2"){file2[line]=1}} file2[substr($0,1,4)]' file1
1245:56:38 389 A
0045:56:58 689 S
0045:55:51 000 Q
1245:56:44 989 E

In the BEGIN section we read "file2" and set an array element with the key being the entire line to be true.

The main section consists of just a test for the array element in file2 identified by the first 4 characters in each line.

awk beginners might write that 'main' part as:
{ key = substr($0,1,4); if (file2[key]==1) { print } }

Cheers,
Hein.