Perl for the win.

#!/usr/bin/perl -w

$first = 1;
while (<>) {
   chomp;
   if ($first) {
     print "$_\n";
     $first = 0;
   }
   $first = 1 if (/^$/);
}

Then

perl test.pl test.txt

On Thu, Jan 11, 2024 at 7:29 AM Rich Shepard <[email protected]>
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