Linux - Newbie This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
09-15-2009, 07:40 AM
|
#1
|
|
LQ Newbie
Registered: Jan 2009
Posts: 17
Rep:
|
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
|
|
|
|
09-15-2009, 09:44 AM
|
#2
|
|
Senior Member
Registered: Aug 2009
Posts: 3,497
|
Maybe use 'z' (zero length) test -
if [ -z $filename]
then
...
cheers
|
|
|
|
09-15-2009, 03:15 PM
|
#3
|
|
Member
Registered: Sep 2009
Distribution: Fedora
Posts: 835
Rep: 
|
Quote:
Originally Posted by abharsair
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.
|
|
|
|
09-15-2009, 05:21 PM
|
#4
|
|
LQ Newbie
Registered: Jan 2009
Posts: 17
Original Poster
Rep:
|
thank you both guys will test this in the morning 
|
|
|
|
09-15-2009, 09:19 PM
|
#5
|
|
LQ Newbie
Registered: Apr 2007
Posts: 1
Rep:
|
-s whether the file exists and has a size greater than zero.
|
|
|
|
09-16-2009, 04:56 AM
|
#6
|
|
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
|
From man bash:
Quote:
|
${#name[subscript]} expands to the length of ${name[subscript]}.
|
Quote:
Originally Posted by lutusp
...
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"?
|
|
|
|
09-16-2009, 05:07 AM
|
#7
|
|
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 3,971
|
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.
|
|
|
|
09-16-2009, 07:53 AM
|
#8
|
|
Senior Member
Registered: Aug 2009
Posts: 3,497
|
true.. my mistake 
|
|
|
|
09-16-2009, 11:49 AM
|
#9
|
|
Member
Registered: Sep 2009
Distribution: Fedora
Posts: 835
Rep: 
|
Quote:
Originally Posted by archtoad6
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.
|
|
|
|
09-17-2009, 02:45 AM
|
#10
|
|
LQ Newbie
Registered: Jan 2009
Posts: 17
Original Poster
Rep:
|
now I am 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
|
|
|
|
09-17-2009, 07:22 AM
|
#11
|
|
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 3,971
|
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 05:27 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|