Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I need to take text like this:

A234321=http://www.google..... a normal URL

And pull out only the URL, getting rid of the first part. I think I can use expr to do it, but I can't figure out the right way

The entire regex I can use is http:[a-zA-Z0-9/_]+

share|improve this question

3 Answers 3

I'm not sure if you're looking for this. See if it helps.

expr 'A234321=http://www.google&x=y' : '^[^=]*=\(.*\)'
share|improve this answer
    
That's exactly what I was looking for!, it works like a charm! What does it mean this part? '^[^=]*= Thanks, Marco –  Marco Aug 30 '11 at 14:18
    
@Marco ^ means "at the beginning of the line". [] is a character class, so it can match a set of characters. The ^ inside it means "match all characters except these", so [^=] means "any character except =". * means "0 or more times". So the whole thing is "At the beginning of the line, match 0 or more characters that aren't =, followed by a single =", so it matches everything from the beginning of the line to the first = character –  Michael Mrozek Aug 30 '11 at 15:12

You don't need expr for that. You can use the shell constructs ${VAR#PATTERN} which expands to $VAR with the shortest prefix that matches the specified pattern stripped off, ${VAR##PATTERN} which strips the longest prefix, and ${VAR%PATTERN} and ${VAR%%PATTERN} which strip suffixes.

text='A234321=http://www.example.com/wibble'
protocol=${text%%://*}
url=${protocol##*[!a-z]}://${text#*://}
share|improve this answer

Assuming your text is always separated by an '=' character, the cut will give you the URL:

url=`echo $text | cut -f 2 -d"="`

Basically, here we are cutting the text field, delimited by = and extracting the 2nd field.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.