Here's some examples showing what's matched between `^` and `$` when the
string in memory (the current awk record) is a line, a multi-line
paragraph, and a whole file, all based on the value of `RS`:
The sample input (courtesy of Robert Burns "Tam O'Shanter" written/set
near my home town):
$ cat file
Ah, gentle dames! it gars me greet,
To think how mony counsels sweet,
How mony lengthen'd, sage advices,
The husband frae the wife despises!
Read 1 line at a time:
$ awk 'match($0,/^.*$/) { print "<" substr($0,RSTART,RLENGTH) ">"
}' file
<Ah, gentle dames! it gars me greet,>
<To think how mony counsels sweet,>
<>
<How mony lengthen'd, sage advices,>
<The husband frae the wife despises!>
Read 1 paragraph at a time:
$ awk -v RS='' 'match($0,/^.*$/) { print "<"
substr($0,RSTART,RLENGTH) ">" }' file
<Ah, gentle dames! it gars me greet,
To think how mony counsels sweet,>
<How mony lengthen'd, sage advices,
The husband frae the wife despises!>
Read the whole file at once (RS='^$` could be `RS='anything nonexistent'`):
$ awk -v RS='^$' 'match($0,/^.*$/) { print "<"
substr($0,RSTART,RLENGTH) ">" }' file
<Ah, gentle dames! it gars me greet,
To think how mony counsels sweet,
How mony lengthen'd, sage advices,
The husband frae the wife despises!
>
As you can see `^` and `$` always match the start and end of the record
that awk is currently processing, whether that's a line, or a paragraph,
or a whole file. The only time when `^` and `$` also identify the
start/end of a line is when the whole record is a single line.
To find lines in the multi-line paragraph case, you need to test for
`^|\n` at the start of the lines (`^` to find the start of the first
line, `\n` to find subsequent) and/or `\n|$` at the end of the lines
(`$` to find the end of the last line, `\n` to find previous), depending
on what you want to do with it, and you also need to account for the
fact that if you find `\n` it'll be part of the matching string, unlike
`^` or `$`, e.g.
$ awk -v RS='' 'match($0,/^[^\n]*/) { print "<"
substr($0,RSTART,RLENGTH) ">" }' file
<Ah, gentle dames! it gars me greet,>
<How mony lengthen'd, sage advices,>
$ awk -v RS='' 'match($0,/\n[^\n]*/) { print "<"
substr($0,RSTART+1,RLENGTH-1) ">" }' file
<To think how mony counsels sweet,>
<The husband frae the wife despises!>
Regards,
Ed.