1831472 Members
3414 Online
110025 Solutions
New Discussion

Re: loop script help

 
Brian Lee_4
Regular Advisor

loop script help

I would like to build the script to
create the output file containg the following information from all of .csv files in /admin directory. The output file convention is "transmission_YYYYMMDDHHMMSS.txt"

name of a file size of a file lines in a file

example)
$ cat /admin/transmission_20040615112003.txt

06152004191647.csv 77KB 1257
06152004102629.csv 171KB 2800

Thank you.
brian lee
4 REPLIES 4
Heiner E. Lennackers
Respected Contributor

Re: loop script help

# wc -lc *.csv | awk '{printf "%s %dKB %d\n",$3,$2/1024,$1}' > /admin/output.txt
if this makes any sense to you, you have a BIG problem
Victor Fridyev
Honored Contributor

Re: loop script help

#!/usr/bin/sh
OTPF=transmission_$(date +%Y%m%H%M%S).txt
cd /admin
/bin/ls -1 *cvs|while read CVS;do
SIZE=$(/bin/ls -ls $CVS|awk '{printf("%d KB",$6/1024)}'
STR=$(cat $CVS |wc -l)
echo $CVS $SIZE $STR
done > $OTPF
Entities are not to be multiplied beyond necessity - RTFM
Sridhar Bhaskarla
Honored Contributor

Re: loop script help

Hi,

Heiner got a good solution for you as he hit size and count in one shot. Just replace "> /admin/output.txt " with

> /admin/transmission_$(date +%Y%m%d%H%M%S).txt

You will have to add 'cd /admin' before it if you are planning to run this script through cron or so.

-Sri

You may be disappointed if you fail, but you are doomed if you don't try
Muthukumar_5
Honored Contributor

Re: loop script help

hai,

The creation of transmission file contains the informations with seconds and minutes. The completion of .csv file search operation will take more seconds. So don't predefine the file with some variable in the shell. Use that at redirection.

#!/usr/bin/ksh
set -x

/usr/bin/ls -l *.csv | awk '{ print $9 }' | while read file; do
/usr/bin/ls -l $file | awk '{ print $9 " "$5/1024" KB "$5 ""}'
done > /admin/transmission_$(date +%Y%m%H%M%S).txt

Regards,
Muthukumar
Easy to suggest when don't know about the problem!