Unix cat starting from line

40

8

What is the best way to output from a file starting from a specific line (big number like 70000). Something like:

cat --line=70000 <file>

vonhogen

Posted 2009-10-30T08:21:12.197

Reputation: 1 949

Answers

60

Take a look at tail, more precisecly, it's --lines=+N switch:

tail --lines=+100 <file>

Svend

Posted 2009-10-30T08:21:12.197

Reputation: 2 354

Wow. I didn't know this even after using this on linux for 8 years ! I always used a bash fn ! Thanks !

getFromLine () { lineno=wc -l $1 | awk '{print $1}' ; lineno=expr $lineno - $2 ; tail -n $lineno $1 ; } – secureBadshah – 2009-10-30T08:53:34.783

2As a note, this does not work on Mountain Lion (Darwin Kernel Version 13.1.0). The variant for Mountain lion is tail -n – Kaushik Ghose – 2014-04-08T15:42:46.430

22

The most obvious way is tail. The syntax might be slightly different depending on what OS you are using:

tail -n +70000

If you can not get tail to work, you could use sed, but it might end up slower:

sed -pe '1,69999d'

Chris Johnsen

Posted 2009-10-30T08:21:12.197

Reputation: 31 786

tail worked just fine in MinGW (on a 600 MB text file). The runtime was only a few seconds (but the input file could have been in the file cache already). – Peter Mortensen – 2016-06-15T22:17:14.187

2

You can use NR parameter with the awk command:

cat <file> | awk '{if (NR>=7000) print}'

Gefolge

Posted 2009-10-30T08:21:12.197

Reputation: 555

2You can use this command with other limits. As a sample:cat messages | awk '{if (NR>=7000 && NR <7003) print}' shows you row 7000, 7001 and 7002 only. – Gefolge – 2017-06-30T06:26:37.630

2

If instead of a line number you need to start listing at the line containing a given $phrase, try the following.

more -1000 +/"$phrase" yourfilename | sed '1,4d'

The -1000 will continuously list text for up to 1000 lines; you can change this as needed. The sed command will chop off the first 4 lines of output, which were automatically inserted by more, containing a blank line, the message "... skipping", and the two lines preceding your intended starting line. I guess this may vary depending on your system.

AlohaUnixFan

Posted 2009-10-30T08:21:12.197

Reputation: 21

-1

satish

Posted 2009-10-30T08:21:12.197

Reputation:

The link is broken - ...can't find the server at www.scripterworld.com. – Peter Mortensen – 2016-12-17T18:42:00.417

2fascinating link, but it doesn't really give any information that applies to this question, and your tail suggestion is the same as the accepted and other answers from weeks ago. why bother to post? – quack quixote – 2009-11-18T05:48:47.540