How to only grab links from text file?

Discussion in 'Mac Programming' started by bingefeller, Jul 4, 2016.

  1. bingefeller macrumors 6502

    Joined:
    Jun 25, 2007
    Location:
    Northern Ireland
    #1
    I have a chatroom log file with a bunch of imgur links buried in there and I'd like to get all of the links and export them to another text file so I can download them.

    I used grep command to get them but it is giving me the entire line of text on which the link appeared, not just the link itself on a single line, if that makes sense.

    Is there any way I can do this?
     
  2. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #2
    I guess you'd need to take the lines containing the links and find a way of chopping out the bit you're interested in. Tools such as 'sed' and 'awk' may help you, but it's difficult to be specific without seeing the text you're dealing with.
     
  3. bingefeller thread starter macrumors 6502

    Joined:
    Jun 25, 2007
    Location:
    Northern Ireland
    #3
    here is an example of the text:

    Code:
    [10Apr12 13:09:03][xxxxxxx] then i believe you're ugly
    [10Apr12 13:09:03][xxxx] https://i.imgur.com/Nuv5ot5.jpg
    [10Apr12 13:09:03][xxxxx] that makes no sense
    It's a chat room log and the file has hundreds of imgur links that I want to chop out.

    Thank you.
     
  4. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #4
    Like superscape already said you could use awk for this but instead of giving it to you on a platter lets make it a little fun. Here's a possible example. I've left out e.g. substituted a few things. You need to replace those things with the relevant parts.

    Code:
    awk '/regex/ { print field }' path/to/your/logfile.txt > path/to/newfile.txt
    Regex you should know from the grep command you used and field shouldn't be to difficult if you read the quote from the awk manual page. The > path/to/newfile.txt part redirects the output of the awk command to a newfile e.g all the URL's.

    Have fun.
     
  5. jaduff46 macrumors regular

    jaduff46

    Joined:
    Mar 3, 2010
    Location:
    Northern NJ
    #5
    I'm with Kryten2 in approach. IMO the only way you develop skill in an area is by is by picking up the appropriate tools and iterating through attempts until you get what you want. grep, awk and sed are the right tools and I've used them countless times over the past 30 years to extract and summarize data from text files.

    And the next time you have a similar need you'll have a bit more knowledge of how to do it and be faster at it.

    It really is fun once you get the hang of it. Like solving a puzzle!
     
  6. bingefeller thread starter macrumors 6502

    Joined:
    Jun 25, 2007
    Location:
    Northern Ireland
    #6
    Once again Kryten2 thank you.

    Thanks to you too jaduff46.

    Best wishes.
     
  7. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #7
    I like this way of responding to questions. I may adopt it myself!
     

Share This Page