简体   繁体   中英

awk to change the record separator (RS) to every 2 lines

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.

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

Divide&Conquer: do it in two steps:

  1. use awk to introduce blank line
    to separate each two-line record: NR%2==0 {print ""}
  2. 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.

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 

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM