[Solved] Syntax error for awk in a loop

can some one please tell me what is the problem with my syntax:confused:

I have 100 files in one folder

  1. want to read each of the line by line
  2. calculate their number of the words between the first word and the last word of each line
  3. create file for each file with number of words between them instead of words for each line
FILES="word/*"
for X in "$FILES"
do	
	awk '{print NF-2}' $X > count/$X-new.txt 
done 

and it gives the error

but some who this is not working and I dont understand where the problem is :wall:

what is the value of the variable ?

$new

And do you have a folder called count inside the test directory ?

no specific value ...

i might be doing this wrong ... I want to create documents with either the same name or something like previous name + new
not sure if i have done right

I have test folder with two folders of data and count
data is the one with 100 files in it and I was running the code when i was on test

i have changed the folders around but still get the same things

try this..

FILES="word/*"
for X in "$FILES"
do
    new_name=$(basename $X)    
    awk '{print NF-2}' $X > count/${new_name}_new.txt 
done

Thanks for the reply

I get the following error

and it only creates one file with combined count and 100 files

Am i missing a loop for that?

can you post your script

Sure

now that change to yours I have

FILES="word/*"
for X in "$FILES"
do
    names=$(basename $X)    
    awk '{print NF-2}' $X > count/${names}.txt 
done  

I have 100 files in WORD
and want the same 100 files in COUNT but number of words instead of the words itself

and i get the following error

basename: extra operand `word/this-that.txt'
Try `basename --help' for more information.

  • does not work inside quotes. Remove the quotes to allow * to expand.
for X in $FILES
do
    names=$(basename $X)    
    awk '{print NF-2}' $X > count/${names}.txt 
done

wow.... it works

cant believe it was such a small thing

i was busy with it the whole day

Thank you so much for your help