Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASH - How to retrieve a single line from the file?

Tags:

bash

How to retrieve a single line from the file?

file.txt
"aaaaaaa"
"bbbbbbb"
"ccccccc"
"ddddddd"

I need to retrieve the line 3 ("ccccccc") Thank you.

like image 483
Charlie Avatar asked Jan 20 '26 11:01

Charlie


1 Answers

sed is your friend. sed -n 3p prints the third line (-n: no automatic print, 3p: print when line number is 3). You can also have much more complex patterns, for example sed -n 3,10p to print lines 3 to 10.

If the file is very big, you may consider to not cycle through the whole file, but quit after the print. sed -n '3{p;q}'

like image 64
Jo So Avatar answered Jan 23 '26 19:01

Jo So