HomeLeetcode195. Tenth Line - Leetcode Solutions

195. Tenth Line – Leetcode Solutions

Description

Given a text file file.txt, print just the 10th line of the file.

Examples:

Assume that file.txt has the following content:

Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
Your script should output the tenth line, which is:

Line 10

Solution 1: Using sed

Bash
sed -n '10p' file.txt

sed -n '10p' file.txt: The -n flag suppresses automatic printing, and 10p tells sed to print the 10th line.

Solution 2: Using awk

Bash
awk 'NR == 10' file.txt

awk 'NR == 10' file.txt: The NR variable represents the current record (line) number, and this command prints the line where NR equals 10.

Subscribe
Notify of

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Popular