bash awk 将记录分隔符 (RS) 更改为每 2 行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11913622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
awk to change the record separator (RS) to every 2 lines
提问by ibcritn
I am wondering how to use Awk to process every 2 lines of data instead of every one. By default the record separator (RS) is set to every new line, how can I change this to every 2 lines.
我想知道如何使用 awk 来处理每 2 行数据而不是每行数据。默认情况下,记录分隔符 (RS) 设置为每个新行,如何将其更改为每 2 行。
回答by Birei
It depends of what you want to achieve, but one way is to use the getlineinstruction. For each line, read next one and save it in a variable. So you will have first line in $0and second one in even_line:
这取决于您想要实现的目标,但一种方法是使用getline指令。对于每一行,阅读下一行并将其保存在一个变量中。所以你将有第一行$0和第二行even_line:
getline even_line
回答by glenn Hymanman
回答by Theodros Zelleke
Divide&Conquer: do it in two steps:
分而治之:分两步做:
- use awk to introduce blank line
to separate each two-line record:NR%2==0 {print ""} - pipe to another awk process and
set record separator to blank line:BEGIN {RS=""}
- 使用awk引入空行
来分隔每条两行记录:NR%2==0 {print ""} - 管道到另一个 awk 进程并将
记录分隔符设置为空行:BEGIN {RS=""}
Advantage: In the second awkprocess you have all fields of the two lines accessible as $1 to $NF.
优点:在第二个awk过程中,您可以将两行的所有字段作为$1 to $NF.
awk '{print}; NR%2==0 {print ""}' data | \
awk 'BEGIN {RS=""}; {=;print}'
Note:$1=$1is used here to enforce an update on $0(the whole record).
This guaranties that the output prints the two-line record on one line.
Once you modify a field in your program when you process the two-line records this is no longer required.
注意:$1=$1此处用于强制更新$0(整个记录)。
这保证输出在一行上打印两行记录。
一旦在处理两行记录时修改程序中的字段,就不再需要了。
回答by Paused until further notice.
This is a bit hackish, but it's a literal answer to your question:
这有点骇人听闻,但它是对您问题的字面回答:
awk 'BEGIN {RS = "[^\n]*\n[^\n]*\n"} {##代码## = RT; print , $NF}' inputfile
Set the record separator to a regex which matches two lines. Then for each line, set $0to the record terminator (which is what matched the regex in RS). This performs field splitting on FS. The print statement is just a demonstration place holder.
将记录分隔符设置为匹配两行的正则表达式。然后对于每一行,设置$0为记录终止符(它与 中的正则表达式匹配RS)。这对 执行字段拆分FS。打印语句只是一个演示占位符。
Note that $0will contain two newlines, but the fields will not contain any newlines.
请注意,$0将包含两个换行符,但字段将不包含任何换行符。

