Skip to main content
added 664 characters in body
Source Link
Scrutinizer
  • 1.1k
  • 5
  • 7

Try something like:

awk 'NR%2{if(p!=$2){v=$1; p=$2}$1=v}1' FS=, OFS=, file 

This tests if $2 has changed and substitutes $1 on every odd line with the same value for every $2


Added an explanation..

awk ' NR%2 { # On every line where NR (recordnr) mod 2 equals 1 (odd line) if(p!=$2) { # If the variable p (previous) is different from the 2nd field then v=$1 # The new value v should become $1 (1st field) p=$2 # The new variable p becomes the current field 2 } # $1=v # On every odd line field 1 becomes the previously set value } 1 # 1 means true. The default action is to print the record (line) ' FS=, OFS=, file # Set the input and output field separators to a comma. 

Try something like:

awk 'NR%2{if(p!=$2){v=$1; p=$2}$1=v}1' FS=, OFS=, file 

This tests if $2 has changed and substitutes $1 on every odd line with the same value for every $2

Try something like:

awk 'NR%2{if(p!=$2){v=$1; p=$2}$1=v}1' FS=, OFS=, file 

This tests if $2 has changed and substitutes $1 on every odd line with the same value for every $2


Added an explanation..

awk ' NR%2 { # On every line where NR (recordnr) mod 2 equals 1 (odd line) if(p!=$2) { # If the variable p (previous) is different from the 2nd field then v=$1 # The new value v should become $1 (1st field) p=$2 # The new variable p becomes the current field 2 } # $1=v # On every odd line field 1 becomes the previously set value } 1 # 1 means true. The default action is to print the record (line) ' FS=, OFS=, file # Set the input and output field separators to a comma. 
Source Link
Scrutinizer
  • 1.1k
  • 5
  • 7

Try something like:

awk 'NR%2{if(p!=$2){v=$1; p=$2}$1=v}1' FS=, OFS=, file 

This tests if $2 has changed and substitutes $1 on every odd line with the same value for every $2