Hi all,
I have written a scripts which count number of lines in all the files in a directory and write in a text file. I don't know how to format it while writing. OS suns solaris 10
my scripts is as below
for i in /ersdg3/ERS/ERS_INPUT_LOGS/RIO/LOGS/RIO_02-Aug-2012/ *.LOG
do
echo "File size of $i is"
#wc -l $i
wc -l < $i
#sum= $(( sum + $(wc -l < "${i}") ))
done>lines.txt
i want the output like this
count File_name
20|1.csv
45|3.csv
30|4.csv
Any help will be much appriciated
Thanks
pamu
August 3, 2012, 7:12am
2
for i in /ersdg3/ERS/ERS_INPUT_LOGS/RIO/LOGS/RIO_02-Aug-2012/*.LOG
do
wc -l $i
done
RudiC
August 3, 2012, 7:13am
3
you don't need a script. Please try (and comment)
wc -l /ersdg3/ERS/ERS_INPUT_LOGS/RIO/LOGS/RIO_02-Aug-2012/*.LOG|awk 'BEGIN {print "count\tfilename"} {printf "%d\t|%s\n", $1,$2}'
wc will count all the files for you, and awk will do the formatting.
I know this, But i want to put a '|' between count and file name
Have you tried RudiC's solution?
THis is fine but i want to write in a file to store them for future use
rudic:
you don't need a script. Please try (and comment)
wc -l /ersdg3/ERS/ERS_INPUT_LOGS/RIO/LOGS/RIO_02-Aug-2012/*.LOG|awk 'BEGIN {print "count\tfilename"} {printf "%d\t|%s\n", $1,$2}'
wc will count all the files for you, and awk will do the formatting.
pamu
August 3, 2012, 7:17am
7
for i in /ersdg3/ERS/ERS_INPUT_LOGS/RIO/LOGS/RIO_02-Aug-2012/*.LOG
do
wc -l $i | awk '{ print $1, "|", $2 }'
done
RudiC
August 3, 2012, 7:19am
8
...then please add an output redirection to the end of the command (as you did in your example)!
Either way shown will work, you just need to redirect the output to a file using
>> filename
, so for example:
for i in /ersdg3/ERS/ERS_INPUT_LOGS/RIO/LOGS/RIO_02-Aug-2012/*.LOG
do
wc -l $i | awk '{ print $1, "|", $2 }' >> output_filename
done
or
wc -l /ersdg3/ERS/ERS_INPUT_LOGS/RIO/LOGS/RIO_02-Aug-2012/*.LOG|awk 'BEGIN {print "count\tfilename"} {printf "%d\t|%s\n", $1,$2}' >> output_filename
The second way is my preferred way as it is a oneliner.
Edit: Ninja'd by RudiC!
It is fine , can we remove the path because it is writing the full path of the file with the file name
pamu
August 3, 2012, 8:35am
11
try this.....
for i in /ersdg3/ERS/ERS_INPUT_LOGS/RIO/LOGS/RIO_02-Aug-2012/*.LOG
do
wc -l $i | awk -F "/" '{ print $1, "|", $NF }'
done
RudiC
August 3, 2012, 9:23am
12
Adopting pamu's example might behave unexpectedly if entered for the working directory and thus not providing any "/". Pls try
wc -l /src/dir/*|awk 'BEGIN {FS="[/ ]+"; print "count\t|filename"} {printf "%d\t|%s\n", $2, $NF}' >outfile
If you dont like the <TAB> char just remove the \t in the print statements.