sed/grep to extract word between 2 characters

huangapple go评论59阅读模式
英文:

sed/grep to extract word between 2 characters

问题

我的句子是

abcde^10=fgi^11=jkl^12=lmn

这个序列也可以被打乱,例如

abcde^11=jkl^12=lmn^10=fgi

序列也可以是:

12=lmn^11=jkl^10=fgi^abcde

我想打印出 lmn

我已经做到了这一步:

~$ echo abcde^10=fgi^11=jkl^12=lmn|grep -o '\^12=[^)]*\^' -> 这不会打印任何内容
~$ echo abcde^11=jkl^12=lmn^10=fgi|grep -o '\^12=[^)]*\^' -> 这会打印 ^12=lmn^
英文:

My sentence is

abcde^10=fgi^11=jkl^12=lmn 

The sequence can be jumbled also
for eg.

abcde^11=jkl^12=lmn^10=fgi

The sequence can also be :

12=lmn^11=jkl^10=fgi^abcde 

I want to print lmn.

I have gotten till thus far:

~$ echo abcde^10=fgi^11=jkl^12=lmn|grep -o '\^12=[^)]*\^' -> this doesnt print anything 
~$ echo abcde^11=jkl^12=lmn^10=fgi|grep -o '\^12=[^)]*\^' -> this prints ^12=lmn^

答案1

得分: 2

使用GNU awk进行多字符RS的方法:

$ echo 'abcde^10=fgi^11=jkl^12=lmn' | awk -v RS='[\n^]' -F'=' '$1==12{print $2}'
lmn

$ echo '12=lmn^11=jkl^10=fgi^abcde' | awk -v RS='[\n^]' -F'=' '$1==12{print $2}'
lmn

或者使用任何awk:

$ echo 'abcde^10=fgi^11=jkl^12=lmn' | awk -v RS='^' -F'[=\n]' '$1==12{print $2}'
lmn

$ echo 'abcde^10=fgi^11=jkl^12=lmn' | awk -v RS='^' -F'[=\n]' '$1==12{print $2}'
lmn

英文:

With GNU awk for multi-char RS:

$ echo 'abcde^10=fgi^11=jkl^12=lmn' | awk -v RS='[\n^]' -F'=' '$1==12{print $2}'
lmn

$ echo '12=lmn^11=jkl^10=fgi^abcde' | awk -v RS='[\n^]' -F'=' '$1==12{print $2}'
lmn

or using any awk:

$ echo 'abcde^10=fgi^11=jkl^12=lmn' | awk -v RS='^' -F'[=\n]' '$1==12{print $2}'
lmn

<p>

$ echo &#39;abcde^10=fgi^11=jkl^12=lmn&#39; | awk -v RS=&#39;^&#39; -F&#39;[=\n]&#39; &#39;$1==12{print $2}&#39;
lmn

答案2

得分: 1

在GNU awk中,根据你展示的示例,你可以在while循环中使用awkmatch函数和正则表达式(^|\^)12=([^^]*)

awk '
{
  while(match($0,/(^|\^)12=([^^]*)/,arr)){
     print arr[2]
     $0=substr($0,RSTART+RLENGTH)
  }
}
' Input_file
英文:

With your shown samples in GNU awk, you can use match function of awk with regex (^|\^)12=([^^]*) in while loop.

awk &#39;
{
  while(match($0,/(^|\^)12=([^^]*)/,arr)){
     print arr[2]
     $0=substr($0,RSTART+RLENGTH)
  }
}
&#39; Input_file

答案3

得分: 0

如果你想提取在第一个12=和下一个^之间的部分(或者字符串的末尾,不确定),你不需要外部工具。bash的替换就足够了:

s='abcde^10=fgi^11=jkl^12=lmn'
s="${s#*12=}"; echo "${s%%^*}"

或者:

[[ "abcde^10=fgi^11=jkl^12=lmn" =~ .*12=([^^]*).* ]] && echo "${BASH_REMATCH[1]}"

但如果你更喜欢使用sed

sed 's/.*12=\([^^]*\).*//' <<< 'abcde^10=fgi^11=jkl^12=lmn'
英文:

If you want to extract the part between the first 12= and the next ^ or the end of the string (not sure of that) you don't need external utilities. bash substitutions are enough:

s=&#39;abcde^10=fgi^11=jkl^12=lmn&#39;
s=&quot;${s#*12=}&quot;; echo &quot;${s%%^*}&quot;

Or:

[[ &quot;abcde^10=fgi^11=jkl^12=lmn&quot; =~ .*12=([^^]*).* ]] &amp;&amp; echo &quot;${BASH_REMATCH[1]}&quot;

But if you prefer sed:

sed &#39;s/.*12=\([^^]*\).*//&#39; &lt;&lt;&lt; &#39;abcde^10=fgi^11=jkl^12=lmn&#39;

答案4

得分: 0

使用grep和正向后顾断言(仅打印匹配项):

grep -Po '(?<=12=)[^^]+'

使用sed(完全打印没有匹配项的行):

sed -E 's/(^|.*\^)12=([^^]+).*//'
英文:

With grep using positive lookbehinds (prints only matches)

grep -Po &#39;(?&lt;=12=)[^^]+&#39;

With sed (prints lines wihtout matches entirely)

sed -E &#39;s/(^|.*\^)12=([^^]+).*//&#39;

答案5

得分: 0

你是指像这样吗?

echo '
abcde^10=fgi^11=jkl^12=lmn 
abcde^11=jkl^12=lmn^10=fgi
12=lmn^11=jkl^10=fgi^abcde' | 

nawk 'gsub(/.*12=|\^.+/,_)^_'
gawk '$_=$2' FS='.*12=|\^.+'
mawk ++NF OFS= FS='.*12=|\^.+'

lmn 
lmn
lmn
英文:

you mean like this ?

echo &#39;
abcde^10=fgi^11=jkl^12=lmn 
abcde^11=jkl^12=lmn^10=fgi
12=lmn^11=jkl^10=fgi^abcde&#39; | 

nawk &#39;gsub(/.*12=|\^.+/,_)^_&#39;

>
gawk '$_=$2' FS='.*12=|\^.+'
>
mawk ++NF OFS= FS='.*12=|^.+'

lmn 
lmn
lmn

答案6

得分: 0

$ echo 'abcde^10=fgi^11=jkl^12=lmn
abcde^11=jkl^12=lmn^10=fgi
12=lmn^11=jkl^10=fgi^abcde
' | awk -F'(^|^)12=' 'NF>1{sub(/^.*/,"",$NF); print $NF}'
lmn
lmn
lmn

英文:
$ echo &#39;abcde^10=fgi^11=jkl^12=lmn
abcde^11=jkl^12=lmn^10=fgi
12=lmn^11=jkl^10=fgi^abcde
&#39; | awk -F&#39;(^|\\^)12=&#39; &#39;NF&gt;1{sub(/\^.*/,&quot;&quot;,$NF); print $NF}&#39;
lmn
lmn
lmn

huangapple
  • 本文由 发表于 2023年8月9日 14:12:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76865021.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定