0

i have a problem. I want to make a bash script that writes data to new column every time i run script. For example every week i check how many files in each folder i have.

find /home/user/admin/stuff/ -mtime -7 | wc -l >> results.xls

find /home/user/admin/old/ -mtime -7 | wc -l >> results.xls

I run script every Monday, but i don't want to overwrite data. I need that new data will be in new column.

For example:

Week1 Week2 Week3 ...
  2     3     5
  1     2     3
  • im sorry in a new column :)) – Paulius Oct 27 '16 at 9:58
0
#!/bin/bash

output_file=/tmp/results.xls

[ ! -f ${output_file} ] && echo -e "\n\n\n" > ${output_file}
stuff_count=$(find /home/user/admin/stuff/ -mtime -7 | wc -l)
old_count=$(find /home/user/admin/old/ -mtime -7 | wc -l)
now=$(date +%y%m%d)

sed -i "1 s/$/\t$now/" /tmp/out.txt
sed -i "2 s/$/\t$stuff_count/" /tmp/out.txt
sed -i "3 s/$/\t$old_count/" /tmp/out.txt
  • Sed is somehow hard to use when it would come to combine more lines. I would suggest paste. Something like paste results.xls <(date +%y%m%d; find /home/user/admin/stuff/ -mtime -7 | wc -l; find /home/user/admin/old/ -mtime -7 | wc -l) > /tmp/tempresults; mv /tmp/tempresults results.xls. Of course, in case of more complicated output, it could be prepared in some temporary file and then merged with paste file_a file_b. – Kalavan Oct 27 '16 at 11:36
0

You can have a the output in this way.

week="" week=`date` echo $week >> results.xls 
find /home/user/admin/old/ -mtime -7 | wc -l >> results.xls

# cat results.xls
Thu Oct 27 14:16:29 IST 2016
0
2

This is just an example. You can modify it in your way.

  • i'm sorry i mean in a new column – Paulius Oct 27 '16 at 10:00

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.