如何使用sed和awk命令提取模式?

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

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 \newline 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&#39;This is a sample text..
EOF
)
echo "$text" | sed 's/\\n/\n/g' | awk "/-----START/,/-----END/"

注意:在您的脚本中,&quot;\n&quot;实际上就是n。如果您想保留\,您需要写成&quot;\\n&quot;&#39;\n&#39;,或者像上面使用here document。

英文:

You need to first replace two characters \ n by a newline character.

text=$(cat &lt;&lt;EOF
-----START\nabcdEFGHij\nKLMN\nO/PqR\n-----END\n&#39;This is a sample text..
EOF
)
echo &quot;$text&quot; | sed &#39;s/\\n/\n/g&#39; | awk &quot;/-----START/,/-----END/&quot;

Note: in your script, &quot;\n&quot; is just n. If you want to preserve \ , you have to write &quot;\\n&quot; or &#39;\n&#39; or use a here document as above.

答案2

得分: 1

请注意,这是您提供的代码部分,不需要翻译:

-----START\nabcdEFGHij\nKLMN\nO/PqR\n-----END\n&#39;This is a sample text..
EOF
)

echo &quot;$text&quot; | 
awk &#39;
match($0,/-----START\\n.*----END/){
  val=substr($0,RSTART,RLENGTH)
  gsub(/\\n/,ORS,val)
  print val
}
&#39;  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&#39;This is a sample text..
EOF
)

echo &quot;$text&quot; | 
awk &#39;
match($0,/-----START\\n.*----END/){
  val=substr($0,RSTART,RLENGTH)
  gsub(/\\n/,ORS,val)
  print val
}
&#39;  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 &#39;/-----START/,/-----END/{//!p}&#39; | \
            tr -d &#39;\n&#39; | sed s/$/&#39;\n&#39;/

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 &#39;/^-*START$/,/^-*END$/p&#39; &lt;(sed &#39;s/\\n/\n/g&#39; &lt;&lt;&lt;&quot;$sample_text&quot;)
-----START
abcdEFGHij
KLMN
O/PqR
-----END

$ awk &#39;/^-*START$/,/^-*END/$&#39; &lt;(awk &#39;gsub(/\\n/,&quot;\n&quot;)&#39; &lt;&lt;&lt;&quot;$sample_text&quot;)
-----START
abcdEFGHij
KLMN
O/PqR
-----END

$ awk -F&#39;\\\\n&#39; &#39;{for (i=1;i&lt;NF;i++) print $i}&#39; &lt;&lt;&lt;&quot;$sample_text&quot;
-----START
abcdEFGHij
KLMN
O/PqR
-----END

huangapple
  • 本文由 发表于 2023年3月3日 21:36:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627763.html
匿名

发表评论

匿名网友

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

确定