提取两个字符之间的单词,使用sed/grep。

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

sed/grep to extract word between 2 characters

问题

I want to print lmn.

~$ echo abcde^10=fgi^11=jkl^12=lmn|grep -o '^12=[^)]*\^'
~$ echo abcde^11=jkl^12=lmn^10=fgi|grep -o '^12=[^)]*\^'
~$ echo 12=lmn^11=jkl^10=fgi^abcde|grep -o '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

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

$ 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中显示的示例,您可以在awkwhile循环中使用正则表达式(^|\^)12=([^^]*)match函数。

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

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=([^^]).*/\1/' <<< 'abcde^10=fgi^11=jkl^12=lmn'


<details>
<summary>英文:</summary>

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&#39;t need external utilities. `bash` substitutions are enough:

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


Or:

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


But if you prefer `sed`:

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


</details>



# 答案4
**得分**: 0

使用正向后视断言的grep(仅打印匹配项)

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


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

sed -E 's/(^|.^)12=([^^]+)./\2/'


<details>
<summary>英文:</summary>

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;

</details>



# 答案5
**得分**: 0

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;
&gt;

gawk &#39;$_=$2&#39; FS=&#39;.*12=|\\^.+&#39;
&gt;

mawk ++NF OFS= FS=&#39;.*12=|\^.+&#39;

lmn 
lmn
lmn

<details>
<summary>英文:</summary>

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;
&gt;
    gawk &#39;$_=$2&#39; FS=&#39;.*12=|\\^.+&#39;
&gt;
    mawk ++NF OFS= FS=&#39;.*12=|\^.+&#39;
---
    lmn 
    lmn
    lmn



</details>



# 答案6
**得分**: 0

lmn
lmn
lmn

<details>
<summary>英文:</summary>

    $ 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

</details>



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

发表评论

匿名网友

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

确定