Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 525
ibcritn Avatar asked Aug 11 '12 09:08

ibcritn


People also ask

How do I change the record separator in awk?

4.1. 1 Record Splitting with Standard awk By default, the record separator is the newline character. This is why records are, by default, single lines. To use a different character for the record separator, simply assign that character to the predefined variable RS .

What is rt in awk?

RT is set each time a record is read. It contains the input text that matched the text denoted by RS , the record separator. This variable is a gawk extension. In other awk implementations, or if gawk is in compatibility mode (see section Command Line Options), it is not special. A side note about NR and FNR .

What is awk ORS?

Awk ORS is an Output equivalent of RS. Each record in the output will be printed with this delimiter. Following is an awk ORS example: $ awk 'BEGIN{ORS="=";} {print;}' student-marks Jones 2143 78 84 77=Gondrol 2321 56 58 45=RinRao 2122 38 37 65=Edwin 2537 78 67 45=Dayan 2415 30 47 20=

What is record separator?

Record Separators (/V)Specify a comma-delimited string of separator bytes that can be expressed in hex format (for example: /VX5B,X6C,X7C) or character format (for example: /V$,%,@) . The default record separator if none is indicated on the /V command as a single byte, hex15.


2 Answers

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
like image 160
Birei Avatar answered Sep 23 '22 17:09

Birei


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.

like image 36
tzelleke Avatar answered Sep 25 '22 17:09

tzelleke