I want to extract just the lines with specific line numbers from a file (I have about 20-50 line numbers, the file has 30,000 lines). So far, the most concise way I've found to do this is e.g.:
gawk 'BEGIN {split("13193,15791,16891", A, ",")} NR in A' <file_name>
but it seems like I should be able to further reduce the amount of typing involved. I've looked at sed
but I think I need an -n
and a -p
for each line number, also thought about cat -n
with grep
but it's more verbose than the above. Does anyone know a better way?
Sed can be more concise:
sed -n "13193p;15791p;16891p" file_name
Put the list of line numbers in a separate file, then
gawk 'FNR==NR {line[$1]; next} NR in line' line_numbers file_name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With