I have the following text file records.text
IronMan 1 2 3 Batman 1 2 3
I have the following awk command
awk 'BEGIN{ FS="\n"; RS="\n\n"} {print NR, ":", $1, $2}' records.text
I get the following output
1: Ironman 2: 1 3: 2 4: 3 5: 6: Batman 7: 1 8: 2 9: 4
Expected output: 1: Ironman 1 2: Batman 1
Which is wrong. This means RS variable is not picked up and still using default "\n" as the record separator? Anyone else with the same issue? Any solutions?
Unlike gnu awk, OSX's BSD awk does not handle multiple-character record separators. You'll have to try it a different way, handling one line at a time.
From your expression, I do get (after adding missing }
awk 'BEGIN{ FS="\n"; RS="\n\n"} {print NR, ";", $1, $2}' file
1 ; IronMan 1
2 ; Batman
Missing a 1 here, compare to what you like.
PS this also need a gnu awk do to the multiple characters in RS
When you working with record separated by empty lines you should set record selector to nothing.
awk -v RS="" '{print NR, ";", $1, $2}' file
1 ; IronMan 1
2 ; Batman 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With