Git   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用AWK中的2个关键字开始和停止打印大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我有这个 …

#!/usr/bin/awk -f {if($1 == "#BEGIN") while($1 != "#END") print}

当input文件被读入时,我想要输出的是什么。

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中删除最后一行

在linux中基于内容拆分文件

在bash中parsingCSV并分配variables

使用awk将多行文件转换为TSV

如何提取列以释放命令

如何从bash文件删除文本块

使用awk来alignment文本文件中的列?

如何删除一个String输出的前两个单词

grep命令查找小于一个数字的前一个单词

使用awk

尝试:

$ 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

$ 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

分支(跳转)回标签a 。

使awk命令成为一个脚本

方法1

创建这个脚本文件

$ 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.

方法2

创建这个文件

$ 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.

使awk脚本变成一个sHell函数

$ 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.

使sed命令成为一个脚本

创建这个文件

$ 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,请注明来意。
标签: