英文:
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 'abcde^10=fgi^11=jkl^12=lmn' | awk -v RS='^' -F'[=\n]' '$1==12{print $2}'
lmn
答案2
得分: 1
使用在GNU awk
中显示的示例,您可以在awk
的while
循环中使用正则表达式(^|\^)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 '
{
while(match($0,/(^|\^)12=([^^]*)/,arr)){
print arr[2]
$0=substr($0,RSTART+RLENGTH)
}
}
' 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'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 '(?<=12=)[^^]+'
With sed (prints lines wihtout matches entirely)
sed -E 's/(^|.*\^)12=([^^]+).*//'
</details>
# 答案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
<details>
<summary>英文:</summary>
you mean like this ?
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
</details>
# 答案6
**得分**: 0
lmn
lmn
lmn
<details>
<summary>英文:</summary>
$ 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
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论