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 have to write a shell script to get value from a string, something like:

string="abcdef" pathName

How can I use grep command with the use of regular expression to get "pathName"?

share|improve this question
    
using bash ? .... –  guido Sep 15 '12 at 8:46
add comment

1 Answer

GNU grep has the --only-matching option:

echo 'string="abcdef" pathName' |grep --only-matching "pathName"

will output "pathName".

Update with more info on the required pattern:

echo 'string="abcdef" pathName' |grep --only-matching "[[:graph:]]*$"

Here [[:graph:]] is the character class of all non-space characters, * means zero-or-more of the preceding pattern, $ means end-of-line.
So the pattern matches the longest sequence of non-space characters at the end of the line.

share|improve this answer
    
but I don't know what exactly "pathName" is. It can be a sequence of characters anything. In particular, I need a regular expression to get it. –  minhcat_vo Sep 15 '12 at 8:30
    
You need to know something about the input string in order to construct a regex. If there are no space characters in "pathName" you could use "[[:graph:]]*$". –  donothingsuccessfully Sep 15 '12 at 8:39
    
thank you very much... can you explain it to me ? –  minhcat_vo Sep 15 '12 at 8:50
    
@minhcat_vo I've updated my answer to explain the pattern. –  donothingsuccessfully Sep 15 '12 at 9:05
add comment

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.