Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quick Unix command to print non-contiguous lines from a text file?

Tags:

text

shell

unix

sed

Given a text file named "people.txt" which consists of:

Anne
Bob
Carl
Daphne
Erwin
Gary
Heather

How can I use a sed command or similar one-liner which specifies only a set of non-consecutive line numbers in order to filter the contents to this result:

Bob
Erwin
Heather

(Note: ignore the fact that they are alphabetical)

Note that the real file I am using has over 100K lines, so the answer should consider efficiency.

I know I can use:

sed '5q;d' people.txt 

to get only line 5 ("Erwin"), but is there a variation on this argument in which I can specify a list of arbitrary line numbers?

I think this is possible with sed alone, but even after reading through man sed I am having trouble figuring this out. I've been looking at other answers that come very close to doing this but nearly all of them deal with getting either just a single line or contiguous lines (a range of lines), or that use a more complicated bash script; for instance, "Quick unix command to display specific lines in the middle of a file?" and "How can I print specific lines from a file in Unix?".

like image 341
Adam Friedman Avatar asked Dec 15 '22 16:12

Adam Friedman


2 Answers

You can ask for specific lines by number like this:

sed -n '1p;5p;7p' my_file

The -n flag means, "don't print lines by default", and then for each line you want, you specify the line number and the p (print) command.

like image 128
larsks Avatar answered May 21 '23 12:05

larsks


$ awk -v lines="2 4 7" 'index(" "lines" "," "NR" ")' file  
Bob
Daphne
Heather

$ awk -v lines="3 5" 'index(" "lines" "," "NR" ")' file  
Carl
Erwin

The blank chars around lines and NR in the above are necessary so that NR value 9 doesn't match when lines contains 19, for example.

If you don't mind hard-coding the line numbers inside the script you could alternatively do:

awk 'NR~/^(2|4|7)$/' file
like image 42
Ed Morton Avatar answered May 21 '23 14:05

Ed Morton