LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-25-2014, 06:22 AM   #1
jnorbert
LQ Newbie
 
Registered: Mar 2013
Posts: 5

Rep: Reputation: Disabled
(Un)sort grep output


Hello,

I have two files, file1 (which is a pattern list) and file2.

file1 is something like this:

Code:
2  OT
3  OT
1  OT
file2:

Code:
HETATM   1   OT  WAT    1        2.306 -17.930   7.294  0.00 39.98
HETATM   2   OT  WAT    2        1.473 -18.004   4.068  0.00 52.67
HETATM   3   OT  WAT    3      -10.344 -25.298   0.648  0.00 54.45
HETATM   4   OT  WAT    4       -0.233 -18.487   1.892  0.00 62.14
HETATM   5   OT  WAT    5        5.343 -15.072   7.648  0.00 80.40
After
Code:
egrep -f file1 file2
the results are sorted like file2:
Code:
HETATM   1   OT  WAT    1        2.306 -17.930   7.294  0.00 39.98
HETATM   2   OT  WAT    2        1.473 -18.004   4.068  0.00 52.67
HETATM   3   OT  WAT    3      -10.344 -25.298   0.648  0.00 54.45
It is fine, but I would like to retain the order of lines from the pattern list, file1, my desired output would look something like this:

Code:
HETATM   2   OT  WAT    2        1.473 -18.004   4.068  0.00 52.67
HETATM   3   OT  WAT    3      -10.344 -25.298   0.648  0.00 54.45
HETATM   1   OT  WAT    1        2.306 -17.930   7.294  0.00 39.98
I cannot change the order of the pattern list, because that is very important.
Any suggestions? I have searched for sed, awk solutions also, but not found anything useful.
 
Old 08-25-2014, 07:13 AM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 979
Blog Entries: 2

Rep: Reputation: 235Reputation: 235Reputation: 235
Code:
#!/usr/bin/perl -w

open(PATTERN,"<file1") or die();
open(SEARCH,"<file2") or die();

while (<PATTERN>) {
    chomp();
    push(@pattern,$_);
}

while (<SEARCH>) {
    chomp();
    foreach $p (@pattern) {
        if (/$p/)  {
           if (defined($found{$p})) {
               push(@{$found{$p}},$_);
           } else {
               $found{$p}=[$_];
           }
        }
    }
}

foreach $p (@pattern) {
  if (defined($found{$p})) {
    printf("%s\n", join "\n", @{$found{$p}});
  }
}
 
1 members found this post helpful.
Old 08-25-2014, 08:56 AM   #3
jnorbert
LQ Newbie
 
Registered: Mar 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
Seems to be working! Thank you!
 
Old 08-25-2014, 10:15 AM   #4
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 170

Rep: Reputation: 56
Another option

Code:
while read line; do grep "$line" file2; done < file1
 
Old 08-26-2014, 06:46 AM   #5
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 979
Blog Entries: 2

Rep: Reputation: 235Reputation: 235Reputation: 235
If input ever grows to look like
HETATM 12 OT WAT 2 1.473 -18.004 4.068 0.00 52.67
the "2 OT" pattern will match (unless the pattern is extended to include a leading space).

Maybe this is better solved by actual comparison of fields rather than in the style of grep.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] ls grep and sort mohitnarula Fedora 9 10-10-2012 04:49 AM
Using grep -v to sort out a log rhbegin Programming 7 05-11-2011 05:29 PM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 11:31 PM
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 05:56 AM
sort grep output wijnands Linux - Newbie 4 10-09-2004 08:14 AM


All times are GMT -5. The time now is 04:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration