Click to See Complete Forum and Search --> : File listing to database script


Jinx
08-15-2003, 01:44 AM
Gday,

I am currently working on a Bash script to du -h a directory, pipe it to a file and then iterate through the file and send it to an SQL database...

It looks something as follows:

<code>
for i in $mydirectory1 $mydirectory2 $mydirectory3;
do
du -h >> afile;
done

</code>

Now after I have piped it to a file I want to pass it to a Mysql Database, but I can't simply do something like "cat afile |awk 'print{1}' "... cause this will list all the data in that column (the size) in the one row, rather than row by row...

Does anyone have any ideas how I can itterate through that file and add the values to MySql?

The MySql table has three columns... size, path_name and date.

Cya round
Jinx

andysimmons
08-15-2003, 02:17 AM
First, I like to parse stuff out of character-delimited files, not fixed-width, those can cause too many problems. Just man tr if you need information on how to convert those kind of files quickly.

Let's say you make it comma-separated. I think you should just be able to do something like:for a in $(cat file_name); do
col1=$(echo ${a%*} | cut -d, -f 1)
col2=$(echo ${a%*} | cut -d, -f 2)
col3=$(echo ${a%*} | cut -d, -f 3)

...here you would add them in the database....

done
I don't know much about databases, but that will loop through each row in the file, and associate variables with the string in each column. Hope that helps.

andysimmons
08-15-2003, 02:39 AM
Screw manpages. Here's what I do to convert tab-delimited fields to comma-separated values:cat file_name | tr -s [:blank:] ',' > file_name.tmp && mv file_name.tmp file_name
What this does is tr(anslate) -s (by squeezing) [:blank:] (1 or more consecutive blank spaces) ',' (into a comma). The rest is self explanatory.

Jinx
08-17-2003, 08:07 PM
Thanks for the help Andy, it was of great assistance!

Only problem I have now is trying to work out how to comma seperate a tab, instead of spaces (will check man for tr), as the Path column for du has spaces in it... so the values are becoming askew! Aieeee! :P

Thanks again.

Cya round
Jinx

andysimmons
08-18-2003, 09:11 PM
If it's truly tab-delimited, then just do a cut without the -d option. Cut by default uses tab for the delimiter. However, [:blank:] with tr should cover tabs and spaces...is this not working?

bwkaz
08-19-2003, 07:00 PM
It is working, he wants it to not cover spaces. ;)

Something like tr [:tab:] ',' <inputfile >outputfile might work, but I haven't used tr ever, so I don't know if it accepts the [:tab:] stuff or not.