Formatting list of IP addresses into a grep command

Hi

I have an input file with a list of random IP addresses, each on a new line. Below is just an example as I omitted the real IP addresses for obvious reasons.

Input: random_ip.txt

10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1

Basically what I would like to do is to take all the IP addresses in the input file and pipe them into an output file eg. zgrep_ip.sh which in essence is a formatted zgrep command from the input file.

Output: zgrep_ip.sh

zgrep --no-filename -e 10.0.0.1 -e 10.0.0.1  -e 10.0.0.1  -e 10.0.0.1  -e 10.0.0.1  -e 10.0.0.1  *.gz > zgrep.out

I hope it makes sense :confused:

#! /bin/bash
echo -e 'zgrep --no-filename \c' >> zgrep_ip.sh
while read x; do echo -e "-e $x \c" >> zgrep_ip.sh; done < random_ip.txt
echo -e '*.gz > zgrep.out\c'>> zgrep_ip.sh

Thanks for your reply, I get the following error;

: No such file or directoryne 3: random_ip.txt

I hope you're not being deliberate here.
What's the name of your input file? The file containing the list of IP's.. Isn't it random_ip.txt ?

Correct, the file containing the list of IP's is called random_ip.txt, not sure why your script is not picking it up? I executed your script from within the directory where the random_ip.txt file reside.

Hmmm... It works for me. Not sure why it doesn't work for you.
May be, some random control characters have crept in either your script file or input file.

Why don't you use

zgrep -F -f random_ip.txt *.gz

Doesn't that work?

Thanks, will double check.

---------- Post updated at 12:14 PM ---------- Previous update was at 12:12 PM ----------

Seems to be working fine, thanks for that! :b: