I am having trouble with this count script

I have to display the file name followed by the line count then work count. I am able to display it in the opposite order, but can figure out how to switch it.

Can anyone help me with this it would be greatly appreciated.

My code is as follows:

#!/bin/bash
#
#Conts and displays the filename, # of line and #of words

LSFILELIST=`ls -1`

for EACHFILE in "${LSFILELIST}"; do

  echo `${EACHFILE}` | wc -lw  ${EACHFILE}

done

 
for fl in * ; do wc "$fl" | read lc wc cc ; echo $fl $lc $wc ; done

That solution won't work with many shells, since they will run the read in a subshell. So that it will work correctly outside of ksh, you should echo from the same subshell.

Regards,
Alister

Try it this way and see if that is what you want:

LSFILELIST=`ls -1`
for EACHFILE in "${LSFILELIST}";
do
  wc -lw  ${EACHFILE} | awk '{print $3 " " $1 " " $2}'
done
1 Like

I tried this, but it just hangs and I have to use ctr-d to escape it. No idea why?

---------- Post updated at 06:08 PM ---------- Previous update was at 05:56 PM ----------

Thanks I figured it out this helped alot.

bash:

 
for fl in * ; do [[ -f "$fl" ]] && { wc=($(wc "$fl")) ; echo $fl ${wc[0]} ${wc[1]}; } ; done