How do you tell awk to use a #
character by itself in a line as record
separator? The problem is you can't say RS="^#$"
because ^
matches the
beginning of the file, not the beginning of a line, and RS="#\n"
doesn't work either because it matches #
characters that aren't at the beginning of a line.
$ data='#
first record, first field
first record, second field
#
second record, first field#
second record, second field
'
Then print the first field of each record, using RS="#\n"
:
$ printf "%s" "$data" | awk '
BEGIN { RS="#\n"; FS="\n" }
/./ {print $1}
'
first record, first field
second record, first field
second record, second field
The last line is wrong because it's not the first field but the second. The intended output was
first record, first field
second record, first field#
line one#line two#line three
?RS="#"
does what?#\nrecord one\n#\nrecord two
and each record consists of multiple\n
-separated fields.RS='#\n'
should work afaik - although it will treat the initial#
as terminating an empty record (i.e. all theNR
values will be "off" by one)