I’m a newbie to Shell script requirement is need to get row count from a flat(.txt) file and assign to a int variable.
Trying this but not working
FNAME="pn_data_"$month$day$year".txt"
declare -i recordcount=0
for f in $FNAME
do
recordcount+=`wc $f |'{print $1}'`
done
Any suggestions around would be appreciated.
The trick with
wcis that if you supply a file name on its command line, it prints it in the output. To avoid that, feed it standard input. The RHS of the|symbol is weird — maybe you missed outawkin front of the action? But it is overkill: you can simply write:(And I’d not normally bother with the
declareline.)Note that I’m assuming that you have just one file and need the record count from the one file. If you need to process multiple files and get the aggregate count, then you either need to process each file separately in the shell and do arithmetic on the value from
wc, or you runwcon all the files and let it print out the total number of lines at the end (which can be post-processed withsedorawkto get just the number from the last line of output).