Most of what you want/do can be accomplished with awk
. But for the minimum you want:
for i in `ls -1 file.txt | sort`; do echo $i`
grep datetime $i | wc -l ``
grep abc $i | wc -l ``
grep def $i | wc -l ``
grep ghi $i | wc -l ``
grep jkl $i | wc -l ` ; done | cut -c9-500 | awk '{print substr($0,1,11) substr($0,15) }'
awk
is very capable at text processing.
Edit: I'm not sure of what are you doing, but, basicly this does (almost) the same:
awk 'FILENAME != oldfilename {oldfilename = FILENAME; dt = 0 ; a = 0; d = 0; g = 0; j = 0}
/datetime/ {dt++}
/abc/ {a++}
/def/ {d++}
/ghi/ {g++}
/j/ {j++}
END {print FILENAME, dt, a, d, g, j}' *
And it's faster, fewer processes, etc... Basically awk process the file, counts the occurences of the specified strings, and when it finishes the file (after the last line) prints the report.
Changed specs:
for i in `ls -1 file.txt | sort`; do echo $i`
grep datetime $i | wc -l ``
grep abc $i | wc -l ``
grep def $i | wc -l ``
grep ghi $i | wc -l ``
grep jkl $i | wc -l ` ; done | cut -c9-500 | awk '{print substr($0,1,8) " " substr($0,9,4) substr($0,15) }'