Reputation: 220
I am trying to write a simple AWK script which uses empty lines as record separator. I reproduced on my PC the example from the GNU AWK manual Multiple-Line Records. I copy the code below:
# addrs.awk --- simple mailing list program
# Records are separated by blank lines.
# Each line is one field.
BEGIN { RS = "" ; FS = "\n" }
{
print "Name is:", $1
print "Address is:", $2
print "City and State are:", $3
print ""
}
Input is:
Jane Doe
123 Main Street
Anywhere, SE 12345-6789
John Smith
456 Tree-lined Avenue
Smallville, MW 98765-4321
Files are created on UNIX system.
Required output is:
Name is: Jane Doe
Address is: 123 Main Street
City and State are: Anywhere, SE 12345-6789
Name is: John Smith
Address is: 456 Tree-lined Avenue
City and State are: Smallville, MW 98765-4321
Instead, I get a result which is different from the expected one. What I get is:
Name is: Jane Doe
Address is: 123 Main Street
City and State are: Anywhere, SE 12345-6789
Does anybody know why I am getting the wrong result? AWK finds only 1 record instead of 2, do you know why?
Upvotes: 2
Views: 2047
Reputation: 116880
This is to confirm that:
(1) the given program works properly using awk version 20070501
, gawk
, or mawk
, provided the input file has bare newline ('\n') line endings (as opposed to CR LF).
(2) if the input is a DOS text file, then the result is as the OP stated.
Also, if the input file is a DOS text file, an alternative to dos2unix
is to use tr
as illustrated here:
$ tr -d '\r' < input.dos.txt | awk ....
Upvotes: 2