Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am wondering is it possible to echo values like below described? The situation is - there are too many "doc" files named like "PLCI507_01234567.doc", and there is a Form in a text file named "form.txt" looks like below;

--------------------------------------------------------------------
Date: 
Count :
--------------------------------------------------------------------
ID   | Host    | md5sum               | Created   | 
--------------------------------------------------------------------
       |         |                    |           |
     |         |                      |           | 
--------------------------------------------------------------------

I am already able to create files in the same format for each documents but now I want all the information in a single form and the form should be updated with file informations ie. file name, created date, md5sum etc.

md5sum = md5sum PLCI507_01234567.doc | awk '{ print $1 }' #sum of each file

Created = date +"%d/%m/%Y" #date of form update

The updated form.txt should be like below.

--------------------------------------------------------------------
Date: 4/14/2011 
Count : 4 
--------------------------------------------------------------------
ID     | Host    | md5sum                           | Created    | 
--------------------------------------------------------------------
01234567 | PLCI507 | 2b3dbf39a65eb140a163401001e32fb2 | 14/03/2011 | 
01234560 | PLCI512 | 7aa31c83cc1b78fa9ae60460e1eb4731 | 22/04/2011 |
--------------------------------------------------------------------

Please some body help me with this. I am hoping that there must be some way to do it.

Thanks!!

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

Here's a script that will generate this file, and print it nicely with printf.

Just call it like: ./script_name.sh /path/to/docfiles/

#!/usr/bin/env bash

docFiles=$(find "$1" -iname '*.doc')
numFiles=$(echo -e "$docFiles" | wc -l)

sep="--------------------------------------------------------------------------------"
Date="Date: $(date +'%d/%m/%Y')"
Count="Count : $numFiles"

# Start printing file
echo $sep
echo $Date
echo $Count
echo $sep
printf ' %-10s | %-10s | %-33s | %-15s |\n' "ID" "Host" "md5sum" "Created"
echo $sep


for file in $docFiles
do
    docMD5=$(md5sum "$file" | awk '{ print $1 }')
    # This was a bit unclear in the question...
    # if you want file mtime instead... use something like:
    #docDate=$(stat -c %d/%m/%Y "$file")
    #docDate=${docDate%% *}

    docID=$(basename "$file" | sed -e 's/^\(.*\)_.*/\1/')
    docHost=$(basename "$file" | sed -e 's/^.*_\(.*\)\.doc$/\1/')

    # Use today's date
    docDate=$(date +"%d/%m/%Y")
    printf ' %-10s | %-10s | %-33s | %-15s |\n' "$docID" "$docHost" "$docMD5" "$docDate"
done

echo $sep
  • Copy/paste with your favorite editor whatever filename you want to call it.
  • chmod +x script_name.sh
  • ./script_name.sh /path/to/docfiles/
share|improve this answer
    
works fine, but if I run this script in the same directory next time it generates results for old one too.. :( and if if i push the result to a text tile, next time it creates new file but with old informations too... any solution for avoiding old file and update only single file? –  Himalay May 9 '11 at 23:34
    
How exactly should it work? If you run it on all the files in the directory a second time... should it only look for new files? What about changed files? Also, is the "Created" field supposed to be mtime, ctime, or just date +"%d/%m/%Y"? It sounds like you want it to update a single file... what is the expected behavior? –  TrinitronX May 10 '11 at 23:07
    
the directory has lots of files uploaded every minute and yes it should only look for new files then update the record a file. I think it's better to check with Created mtime or md5sum ?? –  Himalay May 11 '11 at 0:27
add comment

it seems you need a loop to access each .doc file

i=0
for f in *.doc
do
    ((i++))
    f=$(basename "$f")

    # process each file
    # ...

    form_content="${form_content}$f_id | $f_host | $md5sum | $Created |\n"
done

if .doc files have fixed length name, you can strip host/id via Parameter Expansion.

f_host="${f:0:7}"
f_id="${f:8:8}"

finally, output the result like:

echo "--------------------"
echo "Date: `date +%d/%m/%Y`"
echo "Count: $i"
echo "--------------------"
echo "ID | Host | md5sum | Created |"
echo "--------------------"
echo -e "$form_content"
echo "--------------------"
share|improve this answer
add 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.