Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print specific lines of a file in Terminal [duplicate]

This seems pretty silly, but I haven't found a tool that does this, so I figured I'd ask just to make sure one doesn't exist before trying to code it up myself:

Is there any easy way to cat or less specific lines of a file?

I'm hoping for behavior like this:

# -s == start && -f == finish
# we want to print lines 5 - 10 of file.txt
cat -s 5 -f 10 file.txt

Even something a little simpler would be appreciated, but I just don't know of any tool that does this:

# print from line 10 to the end of the file
cat -s 10 file.txt

I'm thinking that both of these functionalities could be easily created with a mixture of head, tail, and wc -l, but maybe there are builtins that do this of which I'm unaware?

like image 616
m81 Avatar asked Jul 29 '15 23:07

m81


People also ask

How do I print a specific line of a file in Linux?

Use SED to display specific lines The powerful sed command provides several ways of printing specific lines. The -n suppresses the output while the p command prints specific lines. Read this detailed SED guide to learn and understand it in detail. Sed is a must know command for Linux sysadmins.

How do I copy a particular line in Linux?

Place the cursor on the line you wish to copy. Type yy to copy the line. Move the cursor to the place you wish to insert the copied line. Type p to insert the copied line after the current line on which the cursor is resting or type P to insert the copied line before the current line.

Which command is used to extract specific lines records from a file?

The cut command offers many ways to extract portions of each line from a text file. It's similar to awk in some ways, but it has its own advantages and quirks.

How do I print the last 5 lines of a file in Linux?

Linux Tail Command Syntax Tail is a command which prints the last few number of lines (10 lines by default) of a certain file, then terminates. Example 1: By default “tail” prints the last 10 lines of a file, then exits.


1 Answers

yes awk and sed can help

for lines 5 to 10

awk 'NR>4&&NR<11' file.txt
sed -n '5,10p' file.txt

for lines 10 to last line

awk 'NR>9' file.txt
sed -n '10,$p' file.txt
like image 53
Shravan Yadav Avatar answered Oct 23 '22 06:10

Shravan Yadav