1

I have a file (myFile) with 20000 lines and another file (myIndex) like this containing the lines that must be extracted from myFile:

 4
 10
 1200
 ..
 ..

I want to extract these lines with sed:

sed '10!d' myFile

How can I put the file name instead of 4,10 like this:

sed 'myIndex!d' myFile

Or is there a similar function in awk?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
user1436187
  • 3,252
  • 3
  • 26
  • 59

2 Answers2

2

You can use awk:

awk 'NR==FNR{i[$0];next}i[FNR]' index file

NR==FNR is true as long as we are reading the first while index. We are storing all that line numbers in an array i. On the remaining input we are checking if the line number is stored in i. If that is true, awk prints that line.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
1

Try this with GNU sed:

sed -nf <(sed 's/.*/&p/' myIndex) myFile

-n: suppress automatic printing of pattern space

-f: add the contents of script-file to the commands to be executed

<(...): bash's Process Substitution

Cyrus
  • 84,225
  • 14
  • 89
  • 153