How to grep in order based on the input file

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to grep in order based on the input file
# 1  
Old 09-23-2002
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 first, I did it using the following line:

grep -f file.1 file2 > file.3

However, this didn't give the output in the same order as the input file. Rather it gave me the output using the order of the file.2 that was being searched. So file.3 was output using the sort order of file.2 not file.1 as I had wanted. Smilie

My problem was that I am wanting to compare the output of file.3 to the original file file.1 and wanted the output in the same order as the search file file.1.

This lead me to the following solution. I know it seems simple, but it is very effective to output in the same order as the search file.

for name in `cat file.1 `
grep $name file.2
done > file.3

This gives me file.3 in the same order as file.1 is in. Which I can then paste to file.1 for a report.

I offer this to anyone who needs something like this.

Hope it helps!!!

Smilie Smilie
This User Gave Thanks to Kelam_Magnus For This Post:
# 2  
Old 09-24-2002
Re: How to grep in order based on the input file

Originally posted by Kelam_Magnus

for name in `cat file.1 `
grep $name file.2
done > file.3
Does the "> file.3" on the outside of the done statement actually work? When I first saw the segment of code I thought, "Hey! He's gonna overwrite all those responses with an "echo done!"" Then I looked at it closer.
# 3  
Old 09-24-2002
Yes it works. I actually stumbled upon this a few months ago.

I once pondered why this was. I get the same output whether I use > or >> to write out the file if it is outside the loop.

I believe that it is because the "> file.3" is outside of the loop so ALL of the grepped data goes out together and is written ONCE and not each time the loop runs.

I believe that this is a little faster than writing each time the loop finishes, especially with 2 large files. I only had about 200 lines for file1, but file.2 had almost 1000 lines to be searched.

You would be right if I had put the > file.3 inside the loop, it would overwrite the file each time instead of appending as with >> file.3 would do.

So both of these are the same.

for name in `cat file.1 `
grep $name file.2
done > file.3

for name in `cat file.1 `
grep $name file.2 >> file.3

Smilie Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass arguments based on input file?

This script is running some exe file we are passing three argumnet below custome key word Want to update script based on input files every time it will take argument from input file below is the input files should take this input put it into the script. k.ksh cd /u/kali/temp ... (8 Replies)
Discussion started by: Kalia
8 Replies

2. Shell Programming and Scripting

Want to grep records in alphabetical order from a file and split into other files

Hi All, I have one file containing thousands of table names in single column. Now I want that file split into multiple files e.g one file containing table names starting from A, other containing all tables starting from B...and so on..till Z. I tried below but it did not work. for i in... (6 Replies)
Discussion started by: shekhar_4_u
6 Replies

3. Shell Programming and Scripting

Bash to search file based off user input then create new file

In the below bash a file is downloaded when the program is opened and then that file is searched based on user input and the result is written to a new file. For example, the bash is opened and the download.txt is downloaded, the user then enters the id (NA04520). The id is used to search... (5 Replies)
Discussion started by: cmccabe
5 Replies

4. 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

5. Shell Programming and Scripting

Read input files and merge them in given order and write them to input one param or one file

Dear Friends, I am looking for a shell script to merge input files into one file .. here is my idea: 1st paramter would be outfile file (all input files content) read all input files and merge them to input param 1 ex: if I pass 6 file names to the script then 1st file name as output file... (4 Replies)
Discussion started by: hyd1234
4 Replies

6. Shell Programming and Scripting

File w/ many line pairs--how do I order based on 1st lines only?

I have a file in which the data is stored in pairs of lines. The first line (beginining with ">") is a header, the second line is a sequence. I would like to sort the file by species name. Desired output for the example file: I can use sort -t'_' -k2 to alphabetize headers in the... (1 Reply)
Discussion started by: pathunkathunk
1 Replies

7. UNIX for Dummies Questions & Answers

Order based on timestamp in a single file

Hi All, I have a large text file which is a combination of multiple files. This is what I used and it worked. for i in /home/docs/text/* do cat $i >> Single_File done Now wondering, if there is a way to sort that single large file based on timestamps in ascending order. Text file... (11 Replies)
Discussion started by: prrampalli
11 Replies

8. Shell Programming and Scripting

Move input file based on condition

Hello, I have File1 in a directory A, a File2 in a directory B. If the File2 is not empty Then I have to move File1 from directory A to a directory archive Else no action. Is it possible to do this from one command line? Thank you in advance for your answers. Madi (2 Replies)
Discussion started by: AngelMady
2 Replies

9. Shell Programming and Scripting

grep for certain files using a file as input to grep and then move

Hi All, I need to grep few files which has words like the below in the file name , which i want to put it in a file and and grep for the files which contain these names and move it to a new directory , full file name -C20091210.1000-20091210.1100_SMGBSC3:1000... (2 Replies)
Discussion started by: anita07
2 Replies

10. Shell Programming and Scripting

Copy input file based on condition

Hi, I am new to unix shell programming. I want to write a shell script for a functionality existing in mainframe system. I have one file as below as input 123456 &__987 &12yuq abcdef _ referes to blank condition:whenever the input file is having &__ ,it should be replaced... (4 Replies)
Discussion started by: charan0703
4 Replies
Login or Register to Ask a Question