Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

awk issue with RS in mac command line

Tags:

macos

awk

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?

like image 891
KodeWarrior Avatar asked Jun 18 '26 07:06

KodeWarrior


2 Answers

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.

like image 154
Kevin Avatar answered Jun 20 '26 22:06

Kevin


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
like image 35
Jotne Avatar answered Jun 20 '26 22:06

Jotne