Solved

grep awk text in variable positions text file

Posted on 2013-06-21
3
562 Views
Last Modified: 2013-06-21
I have text files I need to extract website links from, but they are in variable positions.

These lines will all be in one text file, and I need to find and pull out only the links.

Examples:

I received the link and it is http://www.google.com/
Bob sent me the best website and I sent it to him:http://www.yahoo.com
Please update to http://www.dropbox.com and I'll get it back to you asap.

dos or linux or python suggestions?
0
Comment
Question by:fkn
3 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39266773
In Python:

import re

# file read code modified from http://stackoverflow.com/questions/8369219/how-do-i-read-a-text-file-into-a-string-variable-in-python#answer-8369272
with open('C:\input.txt', 'r') as inFile:
	text = "".join(line.rstrip() for line in inFile)

for match in re.findall('http://[^ ]+', text):
	print(match)

Open in new window

0
 

Author Closing Comment

by:fkn
ID: 39266831
On the money.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 39267352
Much easier to do

grep -Po "http://[\w-.]+" file

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Utilizing an array to gracefully append to a list of EmailAddresses
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now