1

I have a file with multiple IP addresses for different websites, however I only want to print out the websites associated with a specific IP and display the website. For example,

128.125.21.1,/www.extra.com/home.html,2323,oiehfoioi,wopwpwp
124.15.21.1,/www.yahoo.com/9998,wjeoiwjo,bbdjksbk

All I need is the websites associated with 128.125.21.1 and I've gotten results using awk to print the entire web address and the IP but I would like to remove the IP How can I combine awk and sed to accomplish this?

1
  • Please edit your question and show us your desired output. What you show aren't valid URLs, do you also want to remove the first /? Also clarify if what you want is to print the second comma-separated field on lines whose first comma-separated field is a specific IP. Finally, it would help if you show us what you've tried.
    – terdon
    Commented Mar 4, 2017 at 17:16

1 Answer 1

4

It looks like what you want can be stated as "print the 2nd comma-separated field from any lines whose first comma-separated field is 128.125.21.1". If so, you can use:

awk -F, '$1=="128.125.21.1"{print $2}' file

On your example, that prints:

$ awk -F, '$1=="128.125.21.1"{print $2}' file
/www.extra.com/home.htm

Of course, that isn't an actual URL. To also remove the leading /, you can use:

$ awk -F, '$1=="128.125.21.1"{print $2}' file | sed 's#^/##'
www.extra.com/home.html
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .