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#
Best Answer
Here's one way of doing it in
awk
:The trick is to set the record separator to either the beginning of the file (
^
), or a newline, followed by a#
and another newline\n
.