Solved

grep awk text in variable positions text file

Posted on 2013-06-21
3
564 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 75

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

815 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

7 Experts available now in Live!

Get 1:1 Help Now