0

I am working on shell script and I want to get a pattern in a string starts and ends with a specific character. As much as I can do was:

a=`grep -oh ^\/ $1`
grep -oh \'$ $a

I know that grep -oh command finds a word in a file and prints it so I tried to find the words starts with / and ends with '. But it still finds words, but I need patterns to find, like if /bin/ls'dir is the string, I have to get /bin/ls. Also like this I can not suppress ' character at the end.

How could it be done?

4

2 回答 2

0

Perl regular expressions are handy with the lookahead assertions:

str="like if /bin/ls'dir is the string, I have to get /bin/ls. "
grep -oP "/.*?(?=')" <<< "$str"
/bin/ls
于 2013-10-11T21:24:21.533 回答
0

Assuming it's a list, not with text:

 #!/bin/bash
echo "/bin/ls'dir"|awk -F\' '{print "dir: "$1"  end: "$NF}'
于 2013-10-11T23:22:14.787 回答