Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grep a file using search parameters from another file

I am trying to use a file containing IP addresses as the basis for searching through a Cisco firewall configuration file. Normally, I would use something like:

for i in $(cat ip.file); do grep $i fw.config; done

But doing that returns absolutely nothing. If I put the above script into a file and execute it with the bash -xv flags, each line returns something like this:

+ for i in '`cat ip.file`'
+ grep $'1.2.3.4\r' fw.config  (each IP address is different)

grep 1.2.3.4 fw.config is exactly what I want to happen, but I get nothing back from this command.

I know of the grep -f option, but that also returns nothing. I am not an experienced coder, so I might be overlooking something obvious.

like image 266
Eric Avatar asked Sep 02 '10 00:09

Eric


1 Answers

It looks like ip.file is in DOS format and has \r\n line endings. Run dos2unix on it to convert to UNIX format. This will get rid of the errant \r carriage returns that are messing up grep.

By the way, you can use grep -f FILE to pass grep a list of patterns to search for. It will then do a single pass searching for any of those patterns.

# After doing `dos2unix ip.file'...
grep -f ip.file fw.config

# Or...
grep -f <(dos2unix < ip.file) fw.config
like image 152
John Kugelman Avatar answered Oct 21 '22 16:10

John Kugelman