0

I'm having a heard time figuring out how to extract the IP information out of an output similar to this:

Fri Jun  9 19:01:54 2017,10.0.0.65,devi1,0,unknown os
Fri Jun  9 19:01:54 2017,10.0.0.55,host1,0,unknown os
Fri Jun  9 19:01:54 2017,10.0.0.35,srv01,0,unknown os
Sat Jun 10 23:11:13 2017,10.0.0.10,switch.domain.com,0,unknown os

Any tips on how I can, from that output, get:

10.0.0.65
10.0.0.55
10.0.0.35
10.0.0.10

Running on Bash 4.3.30 in Linux.

Any help would be greatly appreciated. Thank you very much!

  • awk -F, '{print $2}' file – RomanPerekhrest Jun 13 '17 at 21:29
  • 1
    A note for those who follow: if your data is CSV (comma separated values) as opposed to simply being comma delimited, you'll want to use a csv library, or something like csvtool -- there's a lot more to CSV than commas and text, as it might at first appear. – Barton Chittenden Jun 13 '17 at 21:54
4

While you could do this with awk or sed, for a simple extraction between fixed delimiters cut is probably the best fit:

$ cut -d, -f2 < input
  • 1
    No need to use an indirection ('<'); cut takes a file as an argument. – DopeGhoti Jun 13 '17 at 21:32
3

With awk:

awk -F, '{print $2}' /path/to/input

With cut:

cut -d, -f2 /path/to/input
  • 1
    one approach - from my comment, the other - from @ilkkachu answer – RomanPerekhrest Jun 13 '17 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.