I'm trying to write a fairly concise sed script to remove the first two lines of a file only if they are empty, so the following file:
> cat myfile.in
Line 3
Line 5
Would result in a three line output file:
> cat myfile.out
Line 3
Line 5
This involves combining line ranges and pattern matching and I just can't seem to find any examples of that. I'd also be intersted if anyone could suggest and equally (or more) consise Perl alternative. Many thanks.
Footnote
I should add that I tried 1,2{/^$/d}
which worked absolutely fine on Linux, but under AIX I got:
sed: 0602-404 Function 1,2{/^$/d} cannot be parsed.
and on Solaris I got:
sed: command garbled: 1,2{/^$/d}
Which is a blow because this code has to run on both AIX and Solaris, but not Linux! Shame!
I think this should work with sed (if you want to delete both first lines if both are empty):
sed '1,1{N;/^\n$/d}'
i.e.: 1,1
= goto first line, N
= append next line to pattern space, ^\n$/d
= delete those two lines if they are empty.
If you want to delete the first two lines if they are empty (independently from the other line) you could do:
sed '1,2/^$/d'