10

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.

1
  • This won't fix your problem, but you should always quote variables when they're arguments to a command. It's not a bad idea to use -- to stop option processing when one of the arguments is a variable, too. grep -- "$i" fw.config Commented Sep 2, 2010 at 2:44

3 Answers 3

22

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
Sign up to request clarification or add additional context in comments.

2 Comments

the -f approach also has the advantage that it interprets the search pattern as simple strings -- e.g. the dots in "1.2.3.4" will only match dots, not any character.
This was the answer. The list of IP addresses was grepped from a dns zone file which I downloaded from my dns provider via a web browser. I had no idea that it was in dos format. Good catch. After running dos2unix, grep -f worked as expected. Thanks.
5

GNU grep,

grep -f ip.txt config

Its advisable also not to use for loop with cat. (If you do, you should change IFS to $'\n'). Use while read loop instead.

while read -r line
do
  ....
done <"ip.txt"

Comments

1
for i in $(tr '\r' '\n' < ip.file); do grep $i fw.config; done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.