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.
问题:
回答1:
It depends of what you want to achieve, but one way is to use the getline
instruction. For each line, read next one and save it in a variable. So you will have first line in $0
and second one in even_line
:
getline even_line
回答2:
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=""}
Advantage: In the second awk
process you have all fields of the two lines accessible as $1 to $NF
.
awk '{print}; NR%2==0 {print ""}' data | \
awk 'BEGIN {RS=""}; {$1=$1;print}'
Note:
$1=$1
is 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.
回答3:
If you want to merge lines, use the paste
utility:
$ printf "%s\n" one two three four five
one
two
three
four
five
$ printf "%s\n" one two three four five | paste -d " " - -
one two
three four
five
回答4:
This is a bit hackish, but it's a literal answer to your question:
awk 'BEGIN {RS = "[^\n]*\n[^\n]*\n"} {$0 = RT; print $1, $NF}' inputfile
Set the record separator to a regex which matches two lines. Then for each line, set $0
to 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.
Note that $0
will contain two newlines, but the fields will not contain any newlines.