Specialized grep script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Specialized grep script
# 1  
Old 07-20-2016
Specialized grep script

I have been (unsuccessfully) trying to write a script that will do the following:
  • for each line of an input file (that contains IP address),
  • the script searches the target file for any lines containing said IP address
  • if it finds a line (or lines) it writes the line(s) to output
  • if the line is not found the script just writes the IP address (being searched for) to output

Any assistance would be appreciated.

Last edited by rbatte1; 07-21-2016 at 08:17 AM.. Reason: Set to formatted list rather than plain text list
# 2  
Old 07-20-2016
Linux

I am not sure the format of your files but the script will be like this. If the input_file is large, using AWK instead of while-loop may make sense.

Code:
cat input_file | while read IP; do
  grep -F $IP target_file || echo $IP
done

# 3  
Old 07-21-2016
Thanks much, that worked exactly as needed.
# 4  
Old 07-21-2016
How about
Code:
grep -ofinput_file target_file

?
# 5  
Old 07-21-2016
Quote:
Originally Posted by RudiC
How about
Code:
grep -ofinput_file target_file

?
Hi RudiC,
I don't think so. I think ddirc wants entire lines from target_file for lines in target_file that contain one of the strings in input_file (presumably as a complete word; not just as a substring of a word) AND for any lines in input_file that were not found in target_file, ddirc wants those line from input_file to be printed as well.
Quote:
Originally Posted by MasWag
I am not sure the format of your files but the script will be like this. If the input_file is large, using AWK instead of while-loop may make sense.

Code:
cat input_file | while read IP; do
  grep -F $IP target_file || echo $IP
done

There is no need to start an extra process nor to create a pipeline for this. It can be done more efficiently with just:
Code:
while read IP; do
  grep -F $IP target_file || echo $IP
done < input_file

But, as mentioned above, it isn't clear to me that this is sufficient. If a line in input_file is:
Code:
29.15.17.2

that grep will match the following lines in target_file:
Code:
IP address 129.15.17.2 is on serverA
IP address 29.15.17.29 is on serverX

Hi ddirc,
You said the code MasWag suggested is working for you. Do you want lines like those in the above example to be matched? If not, please give us sample input and a clear description of any field delimiters that can be used to find the boundaries between the start and end of an IP address in target_file and please also show us the corresponding sample output you hope to produce.

Also, knowing what operating system and shell you're using always helps us give you suggestions that will stand a better chance of working in your environment.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep within script

Hi, I found script which compare ciphers with openssl and return back all result in "YES" (for matching) and "NO" (for no match) I want to result only "YES" part which can be achieved using grep but not sure how and where to place in below script" Script:... (1 Reply)
Discussion started by: khuharshree
1 Replies

2. Shell Programming and Scripting

Grep Script

Hi, New to scripting and looking for some help. I am trying to write a script that will search a specified directory for any new or modified files within the last 7 days and display the results. This will be ran daily and emailed? Thanks (18 Replies)
Discussion started by: Con592
18 Replies

3. Shell Programming and Scripting

Need a script to create specialized output file

I need a script to process the (space-separate) values for each line as seen in the below input file and then output the data into an output file as follows. We have been unable to create this using typical bash scripting and cold not find anything out on the internet similar to what we are trying... (3 Replies)
Discussion started by: ddirc
3 Replies

4. Shell Programming and Scripting

Inconsistent `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l`

i have this line of code that looks for the same file if it is currently running and returns the count. `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l` basically it is assigned to a variable ISRUNNING=`ps -eaf -o args | grep -i sfs_pcard_load_file.ksh |... (6 Replies)
Discussion started by: wtolentino
6 Replies

5. UNIX for Dummies Questions & Answers

grep IP script

Hello, I have a command and would like to make that as a script. How do I do that? my command is: grep -Eo +\.+\.+\.+. and i want to have that like grepscript.sh x.txt > IP.txt Can somebody help me? (3 Replies)
Discussion started by: eightball
3 Replies

6. Shell Programming and Scripting

script use min resource ( grep grep)

Hi i wrote script use it as watchdog ( i mean it check another program (pooya) whenever that was killed (closed or crashed) it run another script (pooya_start.sh) to start it, this script work fine and do the job for me , i need help of an expert to tell me (exact command) how to change this... (8 Replies)
Discussion started by: pooyair
8 Replies

7. Shell Programming and Scripting

Need specialized random string generator script

Hi, I need a script that will generate a set of random strings in sequence, with the ability to predetermine the length, quantity, and alphabet of individual string, and to use the outputs of earlier strings in the sequence to define the parameters of later strings. For examples, I might want... (5 Replies)
Discussion started by: vajrajames
5 Replies

8. Shell Programming and Scripting

How to grep sql error in shell script and exit the script?

I need help in the following script. I want to grep the sql errors insert into the error table and exit the shell script if there is any error, otherwise keep running the scripts. Here is my script #!/bin/csh -f source .orapass set user = $USER set pass = $PASS cd /opt/data/scripts echo... (2 Replies)
Discussion started by: allinshell99
2 Replies

9. UNIX for Dummies Questions & Answers

trying to grep the first few lines of a continuos script, and exit the script anyidea

Hi. I am trying to extract the output of the first few lines of a continuos sh script. The when i run it, i wont to grep the the first 20 lines of it for an entry and basically do a crtl z out of it or to that effect, and output the results to a text file. I basically want to script... (5 Replies)
Discussion started by: k00061804
5 Replies

10. UNIX for Dummies Questions & Answers

grep script

hi guys, i was hoping you could help me out with the following im designing a help site for my work colleagues and would like a little script which would do a grep to check if an application is running on unix for example this is what i do on unix ps -ef | grep mqm and this tells me if... (4 Replies)
Discussion started by: drchris
4 Replies
Login or Register to Ask a Question