Operating System - HP-UX
1755802 Members
5409 Online
108838 Solutions
New Discussion юеВ

Print a specific line of a large text ascii file

 
Patrick Chim
Trusted Contributor

Print a specific line of a large text ascii file

Hi,

Is there any QUICK method in command line to print a specific line of a large ascii file (over 4M rows) ?

Regards,
Patrick
18 REPLIES 18
Steven Sim Kok Leong
Honored Contributor

Re: Print a specific line of a large text ascii file

Hi,

1) With the line number:

# cat -n $FILE | cut -c5- | grep "^$LINENO "

2) Without the line number but with the specific contents:

# grep $PATTERN $FILE

To identify the line number after from the grep:

# grep -n $PATTERN $FILE

Hope this helps. Regards.

Steven Sim Kok Leong
Steven Sim Kok Leong
Honored Contributor

Re: Print a specific line of a large text ascii file

Hi,

1) With the line number, you can also do this:

# head -$LINENO $FILE | tail -1

Hope this helps. Regards.

Steven Sim Kok Leong
Ralph Grothe
Honored Contributor

Re: Print a specific line of a large text ascii file

e.g. print line #1325

sed -n '1325p' /your/large/file

e.g. (with a not really big file)

# cat -n /etc/services |sed -n '123p'
123 hacl-hb 5300/tcp # High Availability (HA) Cluster he
artbeat
Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: Print a specific line of a large text ascii file

Of course we should kill the old beasts sed, awk etc. in favour of Perl ;-)

# perl -ne 'print if $.==123' /etc/services
hacl-hb 5300/tcp # High Availability (HA) Cluster heartbeat
Madness, thy name is system administration
Peter Kloetgen
Esteemed Contributor

Re: Print a specific line of a large text ascii file

Hi Patrick,

if like the "old beasts".... Here is a solution with awk:

awk 'NR=1234, NR=1234' input_file

This will only print the line with number 1234.

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
Bart Beeren
Advisor

Re: Print a specific line of a large text ascii file

Yes there is. Just by usage of more:

more +/pattern file #Starts 2 lines above search string

more +linenumber file #Starts 2 lines above desired linenumber

See for more details/options --> man more

BB
Life isn├В┬┤t as simple as it seems
Peter Kloetgen
Esteemed Contributor

Re: Print a specific line of a large text ascii file

Hi Patrick,

if you need to print only one line with a specific pattern in it:

awk '/your_pattern/, /your_pattern/' input_file

Or what exactly do you want?

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
Juan Gonz├бlez
Trusted Contributor

Re: Print a specific line of a large text ascii file

Hi,
don't forget another infamous beast:

$ echo "100p"|ed - my_file

Best regards
Juan
harry d brown jr
Honored Contributor

Re: Print a specific line of a large text ascii file

Patrick,

I noticed no one got the bunny, is there something else you are looking for?

live free or die
harry
Live Free or Die