LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   adding NULL to a 3 part files that is concatenated..... (https://www.linuxquestions.org/questions/linux-newbie-8/adding-null-to-a-3-part-files-that-is-concatenated-755338/)

abharsair 09-15-2009 07:40 AM

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

kbp 09-15-2009 09:44 AM

Maybe use 'z' (zero length) test -

if [ -z $filename]
then
...


cheers

lutusp 09-15-2009 03:15 PM

Quote:

Originally Posted by abharsair (Post 3683526)
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

Examine each file like this:

Code:

data=`cat $filename`

if [ ${#data} -eq 0 ]
then
  echo "NULL" > $filename
fi

This simple method is preferable to examining the filesystem's directory listings only because it takes less development time. If you were going to examine millions of files over a period of years, a more sophisticated approach would be better.

abharsair 09-15-2009 05:21 PM

thank you both guys will test this in the morning :)

mejd 09-15-2009 09:19 PM

-s whether the file exists and has a size greater than zero.

archtoad6 09-16-2009 04:56 AM

From man bash:
Quote:

${#name[subscript]} expands to the length of ${name[subscript]}.

Quote:

Originally Posted by lutusp (Post 3684081)
...
Code:

data=`cat $filename`

if [ ${#data} -eq 0 ]
then
  echo "NULL" > $filename
fi

This simple method is preferable to examining the filesystem's directory listings only because it takes less development time.

Am I missing something?
How is the code in the quoted block simpler than, or different from, using "-z" or "-s"?

i92guboj 09-16-2009 05:07 AM

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.

kbp 09-16-2009 07:53 AM

true.. my mistake :(

lutusp 09-16-2009 11:49 AM

Quote:

Originally Posted by archtoad6 (Post 3684805)
From man bash:

Am I missing something?
How is the code in the quoted block simpler than, or different from, using "-z" or "-s"?

Apart from the fact that -z an -s from "test" don't do the same thing and a newbie might not know this, my approach now looks perfectly terrible and I have no idea what I was thinking, or at what hour.

abharsair 09-17-2009 02:45 AM

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

i92guboj 09-17-2009 07:22 AM

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
  echo "$filename exist and is readable"
  if [ -s "$filename" ]; then
    echo "and has a size greater than zero"
  else
    echo "but has a size equal to zero"
  fi
else
  echo "$filename doesn't exist, or couldn't be read"
fi



All times are GMT -5. The time now is 03:04 PM.