0

I want to do this but it doesn't work

input.csv is like this :

name  number
toto  125
tata  126
popo  126
nono  127

file.csv is like this

number

126
127

desired output.csv is like this :

name  number
tata  126
popo  126
nono  127

for number in $(cat ~/Documents/file.csv) ;
do
    awk -F, '$2 == "$number|cut -d',' -f1"' input.csv >> output.csv
done
anubhava
  • 761,203
  • 64
  • 569
  • 643
Jack
  • 25
  • 3
  • 2
    Thanks for showing your efforts which you have put in order to solve your own problem. Please do add samples of input and expected output too in your question for better understanding of your question. – RavinderSingh13 Jan 09 '21 at 06:29
  • 1
    Does this answer your question? [How do I use shell variables in an awk script?](https://stackoverflow.com/questions/19075671/how-do-i-use-shell-variables-in-an-awk-script) – oguz ismail Jan 09 '21 at 09:54
  • Sorry it doesn't answer my question – Jack Jan 09 '21 at 14:12
  • 2
    You're trying to use shell syntax and call a Unix tool from inside an awk script. awk is not shell. awk and shell are 2 completely different tools, each with their own language, syntax, semantics, scope, etc. You can no more use shell syntax and directly call Unix tools from inside an awk script that you can from inside a C program. – Ed Morton Jan 09 '21 at 15:59
  • 1
    Once you update your question to show why you think you need a shell loop calling awk then we can help you. – Ed Morton Jan 09 '21 at 16:02

1 Answers1

3

Don't use a shell loop. It can be done easily in a single awk like this:

awk 'FNR == NR {seen[$1]; next} $2 in seen' file.csv input.csv > output.csv
cat output.csv

name  number
tata  126
popo  126
nono  127
anubhava
  • 761,203
  • 64
  • 569
  • 643