英文:
How to extract pattern using sed and awk command?
问题
以下是已经翻译好的内容:
我有一个在bash脚本中的文本消息,我试图从中提取模式内的一部分。例如,我的文本字符串如下所示:
(cert_a='aaa', cert_b='-----START\nabcdEFGHij\nKLMN\nO/PqR\n-----END')这是一个示例文本..
这是脚本的一部分:
#!/bin/sh
test="(cert_a='aaa', cert_b='-----START\nabcdEFGHij\nKLMN\nO/PqR\n-----END')这是一个示例文本.."
sample_text=$test
echo $sample_text | busybox sed 's/cert_b=/'cert_b='\\n/g' | awk "/-----START/,/-----END/"
这个脚本将由非根用户执行。对于根用户,它可以正常工作。
现在,我想去除所有\n
换行字符并仅提取-----START,-----END
内的字符串。因此,我的输出应该是:
-----START
abcdEFGHij
KLMN
O/PqR
-----END
请问有谁能告诉我如何解决这个问题?
提前致谢。
英文:
I have a text message in a bash script which I am trying to extract a part of it within a pattern. For example my text string looks like:
(cert_a='aaa',cert_b='-----START\nabcdEFGHij\nKLMN\nO/PqR\n-----END\n')This is a sample text..
This is part of a script:
#!/bin/sh
test="(cert_a='aaa',cert_b='-----START\nabcdEFGHij\nKLMN\nO/PqR\n-----END\n')This is a sample text.."
sample_text=$test
echo $sample_text | busybox sed 's/cert_b='/cert_b='\\n/g' | awk "/-----START/,/-----END/"
This script will be executed by non-root user. It is working fine for root user.
Now I want to remove all \n
ewline characters and extract only the string inside -----START,-----END
. Hence my output should be:
-----START
abcdEFGHij
KLMN
O/PqR
-----END
Can anyone please let me know how to resolve this?
Thanks in advance
答案1
得分: 2
需要首先将两个字符\
n
替换为换行符。
text=$(cat <<EOF
-----START\nabcdEFGHij\nKLMN\nO/PqR\n-----END\n'This is a sample text..
EOF
)
echo "$text" | sed 's/\\n/\n/g' | awk "/-----START/,/-----END/"
注意:在您的脚本中,"\n"
实际上就是n
。如果您想保留\
,您需要写成"\\n"
或'\n'
,或者像上面使用here document。
英文:
You need to first replace two characters \
n
by a newline character.
text=$(cat <<EOF
-----START\nabcdEFGHij\nKLMN\nO/PqR\n-----END\n'This is a sample text..
EOF
)
echo "$text" | sed 's/\\n/\n/g' | awk "/-----START/,/-----END/"
Note: in your script, "\n"
is just n
. If you want to preserve \
, you have to write "\\n"
or '\n'
or use a here document as above.
答案2
得分: 1
请注意,这是您提供的代码部分,不需要翻译:
-----START\nabcdEFGHij\nKLMN\nO/PqR\n-----END\n'This is a sample text..
EOF
)
echo "$text" |
awk '
match($0,/-----START\\n.*----END/){
val=substr($0,RSTART,RLENGTH)
gsub(/\\n/,ORS,val)
print val
}
' Input_file
如果您需要关于代码的解释或有其他问题,请随时提出。
英文:
With your shown samples please try following awk
solution. We could use match
function of awk
and could deal with all requirements within single awk
itself.
-----START\nabcdEFGHij\nKLMN\nO/PqR\n-----END\n'This is a sample text..
EOF
)
echo "$text" |
awk '
match($0,/-----START\\n.*----END/){
val=substr($0,RSTART,RLENGTH)
gsub(/\\n/,ORS,val)
print val
}
' Input_file
答案3
得分: 0
以下是您要翻译的内容:
echo $test awk | \
sed -n '/-----START/,/-----END/{//!p}' | \
tr -d '\n' | sed s/$/'\n'/
输出为 abcdEFGHijKLMNO/PqR
。
- 我猜第一个awk命令是为了实际创建一个多行文本?
- 第二行提取位于两个标签
-----START
和-----END
之间的行。 - 最后一行删除所有
\n
,但在末尾添加一个。
英文:
echo $test awk | \
sed -n '/-----START/,/-----END/{//!p}' | \
tr -d '\n' | sed s/$/'\n'/
gives abcdEFGHijKLMNO/PqR
as output.
-
The first awk command I guess is needed to actually create a multiline text?
-
Second line extracts the lines between the two tags
-----START
and-----END
. -
Last line removes all
\n
but adds a final one at the end.
答案4
得分: 0
$ sed -n ' / ^- * START $ / , / ^- * END $ / p ' < (sed 's/\n/\n/g' <<<"$sample_text")
-----START
abcdEFGHij
KLMN
O/PqR
-----END
$ awk '/^- *START$/,/^- *END$/' <(awk 'gsub(/\n/,"\n")' <<<"$sample_text")
-----START
abcdEFGHij
KLMN
O/PqR
-----END
$ awk -F'\n' '{for (i=1;i<NF;i++) print $i}' <<<"$sample_text"
-----START
abcdEFGHij
KLMN
O/PqR
-----END
英文:
$ sed -n '/^-*START$/,/^-*END$/p' <(sed 's/\\n/\n/g' <<<"$sample_text")
-----START
abcdEFGHij
KLMN
O/PqR
-----END
$ awk '/^-*START$/,/^-*END/$' <(awk 'gsub(/\\n/,"\n")' <<<"$sample_text")
-----START
abcdEFGHij
KLMN
O/PqR
-----END
$ awk -F'\\\\n' '{for (i=1;i<NF;i++) print $i}' <<<"$sample_text"
-----START
abcdEFGHij
KLMN
O/PqR
-----END
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论