My problem is the same as it's here, except I only want the first occurrence, ignore all the rest:
我的问题和它在这里一样,除了我只想要第一次出现,忽略所有其余的:
How to use sed/grep to extract text between two words?
如何使用sed / grep在两个单词之间提取文本?
In his example if it would be:
在他的例子中,如果它是:
input: "Here is a String Here is a String"
But I only care about the first "is"
但我只关心第一个“是”
echo "Here is a String Here is a String" | grep -Po '(?<=(Here )).*(?= String)'
output: "is a String Here is a"
输出:“是一个字符串这里是一个”
Is this even possible with grep? I could use sed as well for the job.
这对grep来说甚至可能吗?我也可以使用sed来完成工作。
Thanks
谢谢
3 个解决方案
#1
1
Your regexp happens to be matching against the longest string that sits between "Here" and "String". That is, indeed, "Here is a String Here is a String". This is the default behaviour of the *
quantifier.
您的正则表达式恰好与“Here”和“String”之间的最长字符串匹配。也就是说,“这是一个String这里是一个字符串”。这是*量词的默认行为。
$ echo "Here is a String Here is a String" | grep -Po '(?<=(Here )).*(?= String)'
is a String Here is a
If you want to match the shortest, you may put a ?
(greediness modifier) just after the *
quantifier:
如果你想匹配最短的,你可以放一个? (贪婪修饰符)恰好在*量词之后:
$ echo "Here is a String Here is a String" | grep -Po '(?<=(Here )).*?(?= String)'
is a
is a
#2
0
To get the first word you can use grep -o '^[^ ]*'
:
要获得第一个单词,您可以使用grep -o'^ [^] *':
echo "Here is a String Here is a String" | grep -Po '(?<=(Here )).*(?= String)' | grep -o '^[^ ]*'
And you can pipe grep to grep multiple times to compose simple commands into complex ones.
并且您可以将grep管道多次grep以将简单命令组合成复杂命令。
#3
0
sed 's/ String.*//;s/.*Here //'
#1
1
Your regexp happens to be matching against the longest string that sits between "Here" and "String". That is, indeed, "Here is a String Here is a String". This is the default behaviour of the *
quantifier.
您的正则表达式恰好与“Here”和“String”之间的最长字符串匹配。也就是说,“这是一个String这里是一个字符串”。这是*量词的默认行为。
$ echo "Here is a String Here is a String" | grep -Po '(?<=(Here )).*(?= String)'
is a String Here is a
If you want to match the shortest, you may put a ?
(greediness modifier) just after the *
quantifier:
如果你想匹配最短的,你可以放一个? (贪婪修饰符)恰好在*量词之后:
$ echo "Here is a String Here is a String" | grep -Po '(?<=(Here )).*?(?= String)'
is a
is a
#2
0
To get the first word you can use grep -o '^[^ ]*'
:
要获得第一个单词,您可以使用grep -o'^ [^] *':
echo "Here is a String Here is a String" | grep -Po '(?<=(Here )).*(?= String)' | grep -o '^[^ ]*'
And you can pipe grep to grep multiple times to compose simple commands into complex ones.
并且您可以将grep管道多次grep以将简单命令组合成复杂命令。
#3
0
sed 's/ String.*//;s/.*Here //'