Ivan Pereira
Ivan Pereira

Reputation: 2219

Output specific line huge text file

I have a sql dump with 300mb that gives me an error on specific line.

But that line is in the middle of the file. What is the best approach?

head -n middleLine dump.sql > output?

Or can i output only the line i need?

Upvotes: 49

Views: 47196

Answers (8)

Toby
Toby

Reputation: 242

This can also be done with Perl:

perl -wnl -e '$. == 4444444 and print and exit;' FILENAME.sql

4444444 being the line number you wish to print.

Upvotes: 3

frankliuao
frankliuao

Reputation: 420

You can also try awk like:

awk 'NR==YOUR_LINE_NO{print}' file_name

Upvotes: 2

Patrick Bergner
Patrick Bergner

Reputation: 623

If sed is too slow for your taste you may also use

cat $THE_FILE | head -n $DESIRED_LINE | tail -n 1

Upvotes: 10

potong
potong

Reputation: 58371

This might work for you:

 sed 'X!d;q' file

where X is the line number.

Upvotes: 3

ata
ata

Reputation: 2065

If the file is long, consider using

sed -n 'X{p;q}' file

Where X is the line number. It will stop reading the file after reaching that line.

Upvotes: 25

norq
norq

Reputation: 1434

If you know a phrase on that line I would use grep. If the phrase is "errortext" use:

$ cat dump.sql | grep "errortext"

Upvotes: -4

You could use sed -n -e 123456p your.dump to print line 123456

Upvotes: 63

Eugene Yarmash
Eugene Yarmash

Reputation: 149736

You can use sed:

sed -n "x p" dump.sql

where x is the line number.

Upvotes: 4

Related Questions