0

I have two text file like this:

list.txt

file 'aavr-001-1.mp4'
file 'aavr-001-2.mp4'
file 'aavr-001-3.mp4'
file 'ccr-010-1.mp4'
file 'ccr-010-2.mp4'
file 'ccr-010-3.mp4'
file 'vvvw2-031-1.mp4'
file 'vvvw2-031-2.mp4'

code.txt

aavr-001
ccr-010
vvvw2-031

My code:

for f in $(cat code.txt); do
   s=$(echo $f)
   cat list.txt | sed '/$s/!d' >$s.txt
done

I was expecting to get these three files with the following content:

aavr-001.txt

file 'aavr-001-1.mp4'
file 'aavr-001-2.mp4'
file 'aavr-001-3.mp4'

ccr-010.txt

file 'ccr-010-1.mp4'
file 'ccr-010-2.mp4'
file 'ccr-010-3.mp4'

vvvm2-031.txt

file 'vvvw2-031-1.mp4'
file 'vvvw2-031-2.mp4'

But in the end I could only get three blank files: aavr-001.txt ccr-010.txt vvvw2-031.txt, not any content in them.

What should I do to fix the problem?

p.s. If I only input like cat list.txt | sed '/aavr-001/!d' >aavr-001.txt, I can get the result I want

spmoon
  • 1
  • 1
  • Variables are only expanded inside double quotes, not single quotes. – Barmar Dec 27 '22 at 07:31
  • What's the reason for `s=$(echo $f)`? This is essentially the same as `s=$f` – Barmar Dec 27 '22 at 07:31
  • Why use `sed`? This is basically `grep "$s" list.txt > "$s.txt"` – Barmar Dec 27 '22 at 07:33
  • Because I found at the beginning of the debugging if I don't use `s=$(echo $f)` processing will have other problems, but just tested again seems to be no longer a problem. Anyway, your last suggestion has solved my problem, thank you. – spmoon Dec 27 '22 at 07:43

0 Answers0