Have a look at the IFS and RS variables in awk.  IIRC, you can specify the
RS as '\n\n' and the IFS as '\n'. So printing the first line in a record
becomes ' { print $1 } ' once those variables are set.

ChatGPT came up with a similar answer.

https://chat.openai.com/share/8b993033-af63-4954-a715-2e5de5291232


I’d try them , but I’m mobile at the moment.

Regards,
- Robert


On Thu, Jan 11, 2024 at 8:29 AM Rich Shepard <rshep...@appl-ecosys.com>
wrote:

> I have a file with two-line paragraphs and want to extract only the first
> line of each paragraph to an output file. Sample input file:
> line 1
> line 2
>
> line 1
> line 2
>
> line 1
> line 2
>
> I thought the awk 'next' statement would do this but my attempts failed.
> For example:
> $ gawk '{ print $0, next, next }' testfile.txt > out.txt
> gawk: cmd. line:1: { print $0, next, next }
> gawk: cmd. line:1:             ^ syntax error
> gawk: cmd. line:1: { print $0, next, next }
> gawk: cmd. line:1:                   ^ syntax error
>
> and
>
> $ gawk '{ print $0, \n, \n }' testfile.txt > out.txt
> gawk: cmd. line:1: { print $0, \n, \n }
> gawk: cmd. line:1:             ^ backslash not last character on line
> gawk: cmd. line:1: { print $0, \n, \n }
> gawk: cmd. line:1:             ^ syntax error
>
> I didn't find an example in my SED AWK book or my web searches. I'm sure
> there must be a way to do this and I'd like to learn how.
>
> TIA,
>
> Rich
>

Reply via email to