-1

I am having a problem while using the bash shell. Here is my linux command code:

for i in `cat linshi`;do sed -i '/$i/d' a.txt;done

The content of linshi is:

aa
bb

The content of a.txt is:

aa:wwersdf12314231234
bb:weorpius2345234523
cc:ertoiu230498234098
dd:234092834asdfkdfkg

I want to delete the first and the second row of a.txt.

But unlucky, I found '/$i/d' is not correct. And I have tried '/\$i/d' and '/"\"$id/', but they are fail again. Who can help me?

stack
  • 791
  • 1
  • 14
  • 23

2 Answers2

1

Instead of using single quotes use double quotes. '' doesn't undergo any variable expansion however double quotes do.

This will work:

for i in $(cat linshi);do sed -i "/$i/d" a.txt;done
apatniv
  • 1,543
  • 9
  • 12
1

Variables aren't expanded inside single quotes, only double quotes.

for i in `cat linshi`; do sed -i "/$i/d" a.txt; done

That said, you could do the same thing with:

grep -vf linshi a.txt
John Kugelman
  • 330,190
  • 66
  • 504
  • 555