Grepping a file based on input from a second file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grepping a file based on input from a second file
# 1  
Old 01-18-2010
Error Grepping a file based on input from a second file

how to grep a file based on another input file

File1

ashu 1 ninetwo hari
qwer 6 givefour jan
fghj 8 noeight mar
vbmi 7 noput feb
--
---

File2

noput
noeight
---
--


Taking the input of grep as File2, a search need to be made in File1 giving File3 as output:

File3

vbmi 7 noput feb
fghj 8 noeight mar


i.e. only the lines from File1 with the matching string in File2 were printed into File3.

Please let me know how can this be accomplished through shell.

Thanks in anticipation
# 2  
Old 01-18-2010
Code:
touch file3
cat file2 | while read a
      grep $a file1 >> file3
done

# 3  
Old 01-18-2010
Code:
#!/usr/bin/ksh
for line in file2
do
   grep $line file1 >> file3
done

# 4  
Old 01-18-2010
use this:-


Code:
nawk 'NR==FNR{a[$3]=$0 ; next} $1 in a{print a[$1]}' File1 File2 > File3

SmilieSmilieSmilie
# 5  
Old 01-18-2010
fgrep -f File2 File1 >File3
# 6  
Old 01-18-2010
I sometime don't understand why people recommend awk and sed when it can be done with simple commands.Code needs to clean and understandable to the person writing it.
# 7  
Old 01-18-2010
Quote:
Originally Posted by dinjo_jo
I sometime don't understand why people recommend awk and sed when it can be done with simple commands.Code needs to clean and understandable to the person writing it.
Code:
touch file3
cat file2 | while read a
      grep $a file1 >> file3
done

Your code is clean but you're using some redundant commands (cat, touch, read), this is sufficient:

Code:
grep -f file2 file1 > file3

Don't try to win the Useless Use of Cat Award. 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

Copy data to new file based on input pattern

Hi All, I want to create a new file based on certain conditions and copy only those conditioned data to new file. Input Data is as it looks below. ORDER|Header|Add|32|32|1616 ORDER|Details1......... ORDER|Details2......... ORDER|Details3......... ORDER|Details4............ (10 Replies)
Discussion started by: grvk101
10 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

Retaining file versions based on input parameter

Hello Forum. I have the following files in one directory: abc_july01_2013.txt abc_july02_2013.txt abc_july03_2013.txt abc_july04_2013.txt abc_july05_2013.txt abc_july06_2013.txt abc_july07_2013.txt abc_july08_2013.txt If I want to be able to keep the last 5 versions of the file and... (4 Replies)
Discussion started by: pchang
4 Replies

5. Shell Programming and Scripting

Get the ipaddress and align based on the input file

Hi All, I have a file contains below contents, "interfacename/subnet: public (or) interfacename/subnet:cluster_interconnect" "en2"/10.185.81.0:cluster_interconnect,"en5"/10.185.81.0:cluster_interconnect,"en6"/169.181.146.0:public... (6 Replies)
Discussion started by: kamauv234
6 Replies

6. Shell Programming and Scripting

Append file based upon user input-- solved

Ok, I have a script with a commandline option that allows the user to add a custom function to the script file. I have tried everything in my limited knowledge of sed to get this to work and keep coming up short. I need sed to search for a line starting with a pattern, I've got that part so far,... (0 Replies)
Discussion started by: DC Slick
0 Replies

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

8. Shell Programming and Scripting

Change in Input feed based on condition file

Sorry Guys for not being able to explain in one of my earlier post. I am now putting my requirement with the input file and desired output file. In the below input file - Transaction code is at position 31:40. Business code is from position 318:321 TSCM00000005837 ... (7 Replies)
Discussion started by: varunrbs
7 Replies

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

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