Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

bingefeller

macrumors 6502a
Original poster
Jun 25, 2007
598
34
Northern Ireland
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?
 
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?

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.
 
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.

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.
 
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.

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.

An input line is normally made up of fields separated by white space, or by regular expression FS. The fields are denoted $1, $2, ..., while $0 refers to the entire line.

Have fun.
 
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!
 
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!

I like this way of responding to questions. I may adopt it myself!
 
  • Like
Reactions: jaduff46
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.