adding NULL to a 3 part files that is concatenated.....
Hi Guys,
I've got a script which currently collects a header, detail and footer csv file from a directory, it currently pulls the date from the header file then concatenates the 3 files with the date and scp's them to a remote location.... At the moment the only files sent have data in the detail part, however there is a requirement to send files with no details. is it possible and if so how (please) to search to detail.csv file for 0 file size (something like -eq 0) and if it is add the word NULL to the file before its concatenated?? Any help at all will be appreciated as i have no idea at the moment... cheers |
Maybe use 'z' (zero length) test -
if [ -z $filename] then ... cheers |
Quote:
Code:
data=`cat $filename` |
thank you both guys will test this in the morning :)
|
-s whether the file exists and has a size greater than zero.
|
From man bash:
Quote:
Quote:
How is the code in the quoted block simpler than, or different from, using "-z" or "-s"? |
That solution and the -z ones are mostly equivalents, both need the file to be dumped into a variable (-z "$filename" won't work, that will only tell you whether $filename (the name of the file) is an empty string or not, but will tell you nothing about the file contents.
So, I'd rather use -s, since both solutions above would imply reading both files and dumping them into a variable, which is a waste and inefficient if you have lots of files. However, this is only ok if you have to check a single file. If you have to search for files of 0-length, better use the find command. |
true.. my mistake :(
|
Quote:
|
now I am confused :confused:
Would something like this work? filesize=`du -s $i"_"detail.csv | awk '{print $2}'` if [ $filesize -eq 0 ] then echo "NULL" > $i"_"detail.csv fi thanks again guys |
There's really no need to do strange things if all you need is to check if the file size is zero. As said, -s does this for you. If $filename is the name of the file:
Code:
if [ -r "$filename" ]; then |
All times are GMT -5. The time now is 03:04 PM. |