Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

A program to calculate statistics outputs its data in this format

Calc_Statistics -input_data
zone|label|non_null_cells|null_cells|min|max|range|mean|mean_of_abs|stddev|variance|coeff_var|sum|sum_abs
1|Category 1|2340|6|0.281038027256727|0.462105994578451|0.181067967321724|0.408569300865444|0.408569300865444|0.0272405706818114|0.000742048691070761|6.66730726564858|956.0521640251|956.0521640251
2|Category 2|1504|11|0.231816699029878|0.428150941850618|0.19633424282074|0.368851480689277|0.368851480689277|0.0237446334053689|0.000563807615555358|6.43745101985139|554.7526269567|554.7526269567
...
...
n

I know how to select the values i want with the cut

calc_statistics -input_data | cut -f1,2,34 -d'|'

and I'll get something like this:

zone|label|non_null_cells|null_cells
1|Category 1|2340|6
2|Category 2|1504|11

And I want to run it for a collection of inputs,eg:

for year in years:
    do 
    for month in months:
       do
       calc_statistics -input_data $year$month_data | cut -f1,2,34 -d'|' >> results.txt
       done
    done

but I can how can I add in each line the $year and $month variable so the result will be similar with the following?

2000|2|1|Category 1|2340|6
2000|2|2|Category 2|1504|11
share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

Print it separately. echo is nicely enough to have an option (-n) which makes it skip the ending newline:

for year in years:
    do 
    for month in months:
       do
       echo -n "$year|$month|"
       calc_statistics | cut -f1,2,34 -d'|' >> results.txt
       done
    done

Alternatively, pass them to calc_statistics and make it print them too (then adjust the cut call appropriately). Your example is a bit odd, since both $month and $year are not used at all.

EDIT: Ah, in that case you could use paste, but it's much simpler to do it with text editing apps. In my case sed:

for year in years:
    do 
    for month in months:
       do
       calc_statistics | cut -f1,2,34 -d'|' | sed "2,$ s/^/$year|$month|/" >> results.txt
       done
    done

What it does is read each line after the first (2,$ signifies the line range) and replaces the beggining of the line (^) with the wanted values. All just before saving the string to the results file.

share|improve this answer
    
Thanks for you answer, but it doesn't work. THe program gives all the results on one go, so it only prints it in the first line –  user528025 Jul 9 '12 at 14:33
    
Try now, this should be it. –  lynxlynxlynx Jul 9 '12 at 14:45
    
yes that trick did the job! Thank you! –  user528025 Jul 9 '12 at 15:06
    
Unfortunately it doesn't work either. Sed adds the year|month without checking if it exists, first. So after a couple runs the first line reads : 2010|09|2010|08|2010|07|2010|06|2010|05|2009|09|2009|08|2009|07|2009|06|2009|05|‌​2008| and so on... –  user528025 Jul 9 '12 at 15:15
    
I see, you are running it over and over again. Wait a bit. –  lynxlynxlynx Jul 9 '12 at 15:19
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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