0

I have a bunch of files, and I would like to see how many columns a file has for all of the files with a certain name. So after reading around in the forum, I wrote the code below, but it doesn't print out the file name. Could somebody kindly point out the problem for me please? Thanks a lot in advance!

for file in $(find ./ -name "*_intersect.txt")
do 
name=$(basename $file) 
awk -v var="$name" '{print name,NF; exit}' $file
done

the expected output could be

file1 4
file2 8
file3 5
zzz
  • 153
  • 8

1 Answers1

2

Change print name to print var.

name is a shell variable. var is the awk variable you're populating with the contents of name when you do -v var="$name". Inside awk you have access to awk variables, not shell variables. See How do I use shell variables in an awk script?.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185