Read a specific line from a file: Difference between revisions

simplify sed script
(Added Delphi example)
(simplify sed script)
Line 1,694:
 
=={{header|sed}}==
To print the seventh line:
<lang sed>sed -n 7p</lang>
To print an error message, if no such line:
sed -n 7p
<lang sed>sed -n '7{p7h;h$!d;}; ${x;s/^$/s/^/Error: no such line/p}'</lang>
</lang>
That is we remember (h) the line, if any, in hold space. At the last line ($) we exchange (x) pattern space and hold space. If the hold space was empty, --replace printit by an error message. (Does not work with empty input, because there is no last line.)
To print error message if no such line
<lang sed>
sed -n '7{p;h;}; ${x;/^$/s/^/Error: no such line/p}'
</lang>
That is we remember (h) the line, if any, in hold space. At last line ($) we exchange (x) pattern space and hold space. If hold space was empty -- print error message.
 
=={{header|Seed7}}==
559

edits