I have a question to add a column title during a loop with awk: I currently have this code, which calculates the mean of each line for each csv file :

for i in *.csv; do  
  awk -F"," 'NR==1 { next }
        { T=0
           for(N=1; N<=NF; N++) T+=$N;
           print T }' $i > Mean$i

So I get files MeanName1.csv, MeanName2.csv, ... containing:

2.5                3.5                ...
4.2                3.4
1.2                3.9
1.2                3.7

I would like to get a single file "Mean" containing:

Name1   Name2   ...
2.5     3.5
4.2     3.4
1.2     3.9
1.2     3.7

I failed to use print FILENAME in the loop.

Do you have any suggestions ? Thank you

  • The way you have written the data in the question makes it look like you have several columns of data in the same file. Can we assume that you only have a single column of data per file, as indicated by your actual code? – Kusalananda Mar 12 at 16:30
  • Correct, I have only a single column of data in each Mean File. – MLdish Mar 12 at 16:44

Joining the output files is easiest done with paste, but I don't think it knows how to add the file names as headers. But we can do that manually to the intermediate output files within the loop, so:

for i in *.csv; do
    echo "${i%.csv}" > "$out"
    awk -F"," 'NR==1 { next }
        { T=0
           for(N=1; N<=NF; N++) T+=$N;
           print T }' "$i" >> "$out"
paste Mean*.csv > All.csv

${i%.csv} is just the value of $i with the suffix .csv removed, so for foo.csv and bar.csv, that gives an output like this:

bar     foo
4.33333 2.5
3.8     3.5

I note you're also skipping the first line of each input line (NR==1 { next }). If they contain useful headers, you could of course use those too by changing that to NR==1 { print; next } and then removing the echo I added above.

  • Exactly what I needed, thanks! (I don't need header of each *.csv files, so I keep echo) – MLdish Mar 12 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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