大佬教程收集整理的这篇文章主要介绍了使用AWK中的2个关键字开始和停止打印,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这个 …
#!/usr/bin/awk -f {if($1 == "#BEGIN") while($1 != "#END") print}
This is a simple test file. #BEGIN these lines should be extracted by our script. Everything here will be copied. #END That should be all. #BEGIN Nothing from here. #END user$ extract.awk test1.txt these lines should be extracted by our script. Everything here will be copied. user$
只有第一组BEGIN和END文本将被复制。 不知道如何做到这一点的最佳方法是。
翻译部分行
从awk脚本中的variablesString中删除最后一行
在bash中parsingCSV并分配variables
使用awk将多行文件转换为TSV
如何提取列以释放命令
使用awk来alignment文本文件中的列?
尝试:
$ awk 'f && /^#END/{exit} f{print} /^#BEGIN/{f=1}' test1.txt these lines should be extracted by our script. Everything here will be copied.
怎么运行的:
f && /^#END/{exit}
如果f不为零,并且该行以#END开始,则@L_801_17@。
f{print}
如果变量f不为零,则打印这一行。
/^#BEGIN/{f=1}
如果这行以#BEGIN开始,则将变量f设置为1。
$ sed -n '/^#BEGIN/{n; :a; /^#END/q; p; n; ba}' test1.txt these lines should be extracted by our script. Everything here will be copied.
怎么运行的:
/^#BEGIN/{...}
当我们到达以#BEGIN开始的#BEGIN ,在花括号中执行命令。 这些命令是:
n
阅读下一行。
:a
定义标签a 。
/^#END/q
如果当前行以#END开始,则@L_801_17@。
p
打印这一行。
n
阅读下一行。
ba
创建这个脚本文件:
$ cat script1 #!/bin/sh awk 'f && /^#END/{exit} f{print} /^#BEGIN/{f=1}' "$1"
这可以被执行为:
$ bash script1 test1.txt these lines should be extracted by our script. Everything here will be copied.
创建这个文件:
$ cat script.awk #!/usr/bin/awk -f f && /^#END/{exit} f{print} /^#BEGIN/{f=1}
运行如下:
$ awk -f script.awk test1.txt these lines should be extracted by our script. Everything here will be copied.
或者,使其可执行:
$ chmod +x script.awk
并执行它:
$ ./script.awk test1.txt these lines should be extracted by our script. Everything here will be copied.
$ extract() { awk 'f && /^#END/{exit} f{print} /^#BEGIN/{f=1}' "$1"; } $ extract test1.txt these lines should be extracted by our script. Everything here will be copied.
创建这个文件:
$ cat script.sed #!/bin/sed -nf /^#BEGIN/{n; :a; /^#END/q; p; n; ba}
使其可执行:
$ chmod +x script.sed
而且,运行它:
$ ./script.sed test1.txt these lines should be extracted by our script. Everything here will be copied.
以上是大佬教程为你收集整理的使用AWK中的2个关键字开始和停止打印全部内容,希望文章能够帮你解决使用AWK中的2个关键字开始和停止打印所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。