Squidly
Squidly

Reputation: 2717

sed single line address seems to go through the whole file

I have a large file of data. Each line is a single record. Sounds like a job for sed.

I want to inspect a few lines of data, one at a time, but they're json with base64 encoded values. To inspect line 2, I run :

sed -n 2p hugeFile | json 'key' | base64 --decode

Which works fine, except that sed seems to carry on going through the file.

Am I using sed incorrectly here, or is it really going through every file, checking each lines to see if it's line 2?

Upvotes: 1

Views: 162

Answers (1)

Barmar
Barmar

Reputation: 780984

You can combine multiple commands with curly braces, and execute the q command to exit immediately.

sed -n '2{p;q;}' hugeFile

It works like this because you might have multiple commands, or an address that isn't just a single line number. sed doesn't optimze the special case where there's just a single command and it's a line number range.

Upvotes: 3

Related Questions