thiagowfx's avatar

Β¬ just serendipity πŸ€ (not just serendipity)

LeetCode #195: Tenth line

β€’ 73 words β€’ 1 min β€’ updated

LeetCode #195: Tenth line:

shell
sed -n '10p' file.txt

-n is necessary to suppress other output.

If we do not know sed, then head + tail is a decent combination.

shell
head -n 10 file.txt | tail -1

The above fails the edge case of <10 lines. The fix is straightforward:

shell
if [[ $(head -n 10 file.txt | wc -l) -ge 10 ]]; then
    head -n 10 file.txt | tail -1
fi