-2

I have such textfile:

313 "88.68.245.12"
189 "87.245.108.11"
173 "84.134.230.11"
171 "87.143.88.4"
158 "77.64.132.10"
....

I want to grep only the IP from the first 10 lines, run whois over the IP adress and from that output I want to grep the line where it says netname.

How can I achieve this?

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Stephan Kristyn
  • 15,015
  • 14
  • 88
  • 147

1 Answers1

1

Just loop through the file with while - read:

while IFS='"' read -r a ip c
do
    echo "ip: $ip"
    whois "$ip" | grep netname
done < <(head -10 file)

This is giving IFS='"' so that the field separator is a double quote ". This way, the values within double quotes will be stored in $ip.

Then, we print the ip and perform the whois | grep thing.

Finally, we feed the loop with head -10 file, so that we just read the first 10 lines.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • As an aside: I am trying to incorporate a sort feature in your script, but this is not working `done < <(head -10 file | sort)` – Stephan Kristyn Jan 13 '15 at 15:02
  • What are you trying to `sort`? The initial file or the output? If it is the output, just pipe everything *after* the whole block I posted. – fedorqui Jan 13 '15 at 15:03
  • TY for following up. Trying to sort the file which has around 1000 of lines then run the whois block on the first ten lines ot that output. – Stephan Kristyn Jan 13 '15 at 15:18
  • @SirBenBenji This is quite a different question and, for this, you should indicate desired output, etc. You'd better check some questions with [sort] and [bash] and also `man sort`. – fedorqui Jan 13 '15 at 15:29
  • Follow-up: http://stackoverflow.com/questions/27941781/bash-script-to-sort-input-file-before-piping-to-while-loop#27941781 – Stephan Kristyn Jan 14 '15 at 12:22