April 09, 2009

UNIX case study - 15

Grepping lines in order

I am stuck on a simple issue but couldn't find a simple solution. If you have any ideas please help.

I have two files : -

FILE1
Tue 09/12 Lindsey
Wed 09/13 Randy
Thu 09/14 Susan
Fri 09/15 Randy
Sat 09/16 Lindsey
Sun 09/17 Susan

FILE2
Fri
09/12
Sat
09/13

I want to grep all the lines from FILE1 which contain the pattern in FILE2 and in the same order of FILE2

I tried ' grep -f FILE2 FILE1'

The output is -
Tue 09/12 Lindsey
Wed 09/13 Randy
Fri 09/15 Randy
Sat 09/16 Lindsey

I am getting all the desired lines from FILE1 but NOT in the order present in FILE2.

Required OUTPUT is : -
Fri 09/15 Randy
Tue 09/12 Lindsey
Sat 09/16 Lindsey
Wed 09/13 Randy


http://www.unix.com/shell-programming-scripting/106745-how-grep-lines-particular-order.html

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.