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

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

How to extract pattern using sed and awk command?

问题

以下是已经翻译好的内容:

  1. 我有一个在bash脚本中的文本消息,我试图从中提取模式内的一部分。例如,我的文本字符串如下所示:
  2. (cert_a='aaa', cert_b='-----START\nabcdEFGHij\nKLMN\nO/PqR\n-----END')这是一个示例文本..
  3. 这是脚本的一部分:
  4. #!/bin/sh
  5. test="(cert_a='aaa', cert_b='-----START\nabcdEFGHij\nKLMN\nO/PqR\n-----END')这是一个示例文本.."
  6. sample_text=$test
  7. echo $sample_text | busybox sed 's/cert_b=/'cert_b='\\n/g' | awk "/-----START/,/-----END/"

这个脚本将由非根用户执行。对于根用户,它可以正常工作。

现在,我想去除所有\n换行字符并仅提取-----START,-----END内的字符串。因此,我的输出应该是:

  1. -----START
  2. abcdEFGHij
  3. KLMN
  4. O/PqR
  5. -----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:

  1. (cert_a='aaa',cert_b='-----START\nabcdEFGHij\nKLMN\nO/PqR\n-----END\n')This is a sample text..

This is part of a script:

  1. #!/bin/sh
  2. test="(cert_a='aaa',cert_b='-----START\nabcdEFGHij\nKLMN\nO/PqR\n-----END\n')This is a sample text.."
  3. sample_text=$test
  4. 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:

  1. -----START
  2. abcdEFGHij
  3. KLMN
  4. O/PqR
  5. -----END

Can anyone please let me know how to resolve this?

Thanks in advance

答案1

得分: 2

需要首先将两个字符\ n替换为换行符。

  1. text=$(cat <<EOF
  2. -----START\nabcdEFGHij\nKLMN\nO/PqR\n-----END\n&#39;This is a sample text..
  3. EOF
  4. )
  5. 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.

  1. text=$(cat &lt;&lt;EOF
  2. -----START\nabcdEFGHij\nKLMN\nO/PqR\n-----END\n&#39;This is a sample text..
  3. EOF
  4. )
  5. 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

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

  1. -----START\nabcdEFGHij\nKLMN\nO/PqR\n-----END\n&#39;This is a sample text..
  2. EOF
  3. )
  4. echo &quot;$text&quot; |
  5. awk &#39;
  6. match($0,/-----START\\n.*----END/){
  7. val=substr($0,RSTART,RLENGTH)
  8. gsub(/\\n/,ORS,val)
  9. print val
  10. }
  11. &#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.

  1. -----START\nabcdEFGHij\nKLMN\nO/PqR\n-----END\n&#39;This is a sample text..
  2. EOF
  3. )
  4. echo &quot;$text&quot; |
  5. awk &#39;
  6. match($0,/-----START\\n.*----END/){
  7. val=substr($0,RSTART,RLENGTH)
  8. gsub(/\\n/,ORS,val)
  9. print val
  10. }
  11. &#39; Input_file

答案3

得分: 0

以下是您要翻译的内容:

  1. echo $test awk | \
  2. sed -n '/-----START/,/-----END/{//!p}' | \
  3. tr -d '\n' | sed s/$/'\n'/

输出为 abcdEFGHijKLMNO/PqR

  • 我猜第一个awk命令是为了实际创建一个多行文本?
  • 第二行提取位于两个标签-----START-----END之间的行。
  • 最后一行删除所有\n,但在末尾添加一个。
英文:
  1. echo $test awk | \
  2. sed -n &#39;/-----START/,/-----END/{//!p}&#39; | \
  3. 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

英文:
  1. $ sed -n &#39;/^-*START$/,/^-*END$/p&#39; &lt;(sed &#39;s/\\n/\n/g&#39; &lt;&lt;&lt;&quot;$sample_text&quot;)
  2. -----START
  3. abcdEFGHij
  4. KLMN
  5. O/PqR
  6. -----END
  7. $ awk &#39;/^-*START$/,/^-*END/$&#39; &lt;(awk &#39;gsub(/\\n/,&quot;\n&quot;)&#39; &lt;&lt;&lt;&quot;$sample_text&quot;)
  8. -----START
  9. abcdEFGHij
  10. KLMN
  11. O/PqR
  12. -----END
  13. $ awk -F&#39;\\\\n&#39; &#39;{for (i=1;i&lt;NF;i++) print $i}&#39; &lt;&lt;&lt;&quot;$sample_text&quot;
  14. -----START
  15. abcdEFGHij
  16. KLMN
  17. O/PqR
  18. -----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:

确定