adding NULL to a 3 part files that is concatenated.....
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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...
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.
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.