Im Having 6 .csv files in my folder as aca1.csv, aca2.csv, aca3.csv, team1.csv, team2.csv and team3.csv. each file has 80,90,60,70,80 and 90 rows.. I need a bash script file where it will combine or merge files with its type for ex.. aca1,aca2,aca3 will be aca type. and Count the row values(without creating new merged file becauset i want to count the rows only). Write the Count value and Type of the file into another .csv file.. If anyone knows help me?

  • 6
    Show us what you have tried so far and where you got stuck. Also, if your teacher placed any special requirements on how you solve this, make them clear. – John1024 Jul 17 '17 at 5:39
  • 2
    Please give examples of both input files and the expected output. Well not all 90 columns but a few to show the princip. – hschou Jul 17 '17 at 7:42
  • What you mean by row values? 1,2,3 and 2,3,4 will give a row with 6 and a row of 9 or just count the rows per file pattern? – igiannak Jul 17 '17 at 8:04

Try the below as a bash script which works for files named as <pattern><number>.<extension> like abc1.csv, abc02.csv, abc123.csv. You can optionally provide the input directory (default: .) as the first command line argument and the file extension (default: csv) as the second; of course, you will have to compulsorily specify $1 if you want to specify $2.

$ cat ./countlinesbyfilecategory.sh

ptrns=$(for fnm in `find ${1:-.} -regex ".*[0-9][0-9]*\.${2:-csv}"`; do echo $fnm | sed "s/[0-9][0-9]*\.${2:-csv}$//" ; done| sort -u)

for ptrn in $ptrns; do
        echo -n $(basename $ptrn) " "
        find ${1:-.} -regex "${ptrn}[0-9][0-9]*\.${2:-csv}" | xargs wc -l | tail -1 | awk '{print $1}'


You could check the following that uses wc awk and a while loop and iterate over different patterns since you want to count the rows only:

while read i
done <<(wc -l aca* | awk '{print $1}')
echo $n;

wc -l aca* | awk '{print $1}' will give you the number of lines per file of the pattern. while reading the above output you will get the n value that is zero and add recursivly the output of wc at last you print the total lines.

for logging the type and rows you youc script it and put as input the pattern you want to measure then echo "$pattern $n" >> output.txt

user@server[/apps/home/user/]>  cat script.sh

for p in "aca*" "*sh"
while read i;
 do ((n=n+$i));
 done < <(wc -l $p | awk '{print $1}'); echo "$p" $n;

user@server[/apps/home/user/]> bash script.sh
aca* 46
*sh 432

It is unclear whether you need the rowcounts of all the types in one or in separate csv files.

If you need two different csv files, then use,

sh-4.3$ for i in aca team
i_ct=`awk -v pat=$i -F ',' 'BEGIN{c=0} FILENAME ~ pat {c += NF}END{print c}' *.csv`
echo "$i,${i_ct}" > ${i}_count.csv 

If you need type and rowcount of both "test" and "aca" in one file

sh-4.3$ for i in aca team 
i_ct=`awk -v pat=$i -F ',' 'BEGIN{c=0} FILENAME ~ pat {c += NF}END{print c}' *.csv`
echo "$i,${i_ct}" 
done  > All_ftype_count.csv

If there are more file types, you can add it after team in for i in . Do share your feedback.

Not the answer you're looking for? Browse other questions tagged or ask your own question.