How to grep lines in the particular order?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to grep lines in the particular order?
# 1  
Old 04-08-2009
Network How to grep lines in the particular order?

Hi guys,
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

Please help if u have some simple solution. Thanks in advance.

Last edited by sam_2921; 04-08-2009 at 03:25 PM.. Reason: formating
# 2  
Old 04-08-2009
I am not aware of a simple solution so would propose a script:
Code:
while read TESTLINE; do
  grep "${TESTLINE}" FILE1
done < FILE2

# 3  
Old 04-09-2009
Thanks Tony. It has solved my problem.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with order lines from a file based on a pattern

Hi I need to order these lines from a txt file my file looks like this IMSI ........................ 1234567890 APN ......................... INTERNET.COM APN ......................... MMS.COM APN ......................... WAP.COM APN ......................... BA.COM IMSI... (4 Replies)
Discussion started by: alone77
4 Replies

2. Shell Programming and Scripting

Sorting lines between patterns in alphabetical order

Hi, need help in sorting lines between strings "<section status = “ole-service”>" and "</section>" in alphabetical order, based on the text in red. Hoping for an AWK or SED solution. Thank you. ... <section status = “ole-service”>... <p service = "OOO">XZZ</p> <p service = "AAA">AAA... (3 Replies)
Discussion started by: pioavi
3 Replies

3. Shell Programming and Scripting

Print selected lines from file in order

I need to extract selected lines from a log file, I can use grep to pull one line matching 'x' or matching 'y', how can I run through the log printing both matching lines in order top to bottom. i.e line 1 xyz - not needed line 2 User01 - needed line 3 123 - not needed line 4 Info - needed... (2 Replies)
Discussion started by: rosslm
2 Replies

4. Shell Programming and Scripting

How to swap order of pairs of lines?

This seems to be a question whose answer uses sed or awk. For a file like: a b c d e How to swap the order of the line pairs, to end up with: b a d c e All lines from the original file need to wind up in the output file. (8 Replies)
Discussion started by: rd5817
8 Replies

5. Shell Programming and Scripting

Order file by lines

My script(3 arguments $1 = folder,$2 extension,$3 string) should do the following things: -Enter in the folder of $1(if exists). -Put ls *.$2 > temp.txt ( I use a temp file to store the result of ls command and if $2 = txt in this file I'll have all the .txt files of the folder) -Now I want to... (2 Replies)
Discussion started by: Max89
2 Replies

6. Shell Programming and Scripting

Keep lines with specific words up in an order

I hava a file with following data: number|CREDIT_ID|NULL date|SYS_CREATION_DATE|NULL varchar2|GGS_COMMIT_CHAR|NULL varchar2|GGS_OP_TYPE|NULL number|GGS_SCN|NULL| number|GGS_LOG_SEQ|NULL number|GGS_LOG_POS|NULL number|GGS_ORACREC_SCN|NULL varchar2|BATCH_ID|NULL char|GGS_IMAGE_TYPE|NULL ... (6 Replies)
Discussion started by: kolesunil
6 Replies

7. Shell Programming and Scripting

reversing order of lines in a file

how can i reverse the line order in text files? (but total number of the lines is not constant ) for example i have a file like this: line1 line2 line3 . . lineN i wantto make it like this: lineN . . . line3 (26 Replies)
Discussion started by: gfhgfnhhn
26 Replies

8. Shell Programming and Scripting

grep - search order

Hi, I would like "grep" command to search ALL files in current directory in the order of their modified/created date. How can i do this? e.g., if 3 files contain the pattern i am searching for, i need the output of "grep" to be in the order such that old file search result should come first.... (6 Replies)
Discussion started by: prvnrk
6 Replies

9. UNIX for Advanced & Expert Users

How to remove duplicate lines of a record without changing the order

Hi all, I have to remove duplicate lines in a file without chainging the order.for eg if i have a record pqr def abc lmn pqr abc mkh hgf the output should be pqr def abc lmn mkh hgf (7 Replies)
Discussion started by: abhi.roy03
7 Replies

10. Shell Programming and Scripting

How to grep in order based on the input file

Here is an answer rather than a question that I thought I would share. In my first attempt, I was using grep to find a list from file.1 within another file, file.2 which I then needed to paste the output from file.3 to file.1 in the same order. However, the results weren't what I wanted. At... (2 Replies)
Discussion started by: Kelam_Magnus
2 Replies
Login or Register to Ask a Question