LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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



Reply
 
Search this Thread
Old 09-15-2009, 08:40 AM   #1
abharsair
LQ Newbie
 
Registered: Jan 2009
Posts: 17

Rep: Reputation: 0
Question 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
 
Old 09-15-2009, 10:44 AM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,758

Rep: Reputation: 644Reputation: 644Reputation: 644Reputation: 644Reputation: 644Reputation: 644
Maybe use 'z' (zero length) test -

if [ -z $filename]
then
...


cheers
 
Old 09-15-2009, 04:15 PM   #3
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 101Reputation: 101
Quote:
Originally Posted by abharsair View Post
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.
 
Old 09-15-2009, 06:21 PM   #4
abharsair
LQ Newbie
 
Registered: Jan 2009
Posts: 17

Original Poster
Rep: Reputation: 0
thank you both guys will test this in the morning
 
Old 09-15-2009, 10:19 PM   #5
mejd
LQ Newbie
 
Registered: Apr 2007
Posts: 1

Rep: Reputation: 0
-s whether the file exists and has a size greater than zero.
 
Old 09-16-2009, 05:56 AM   #6
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
From man bash:
Quote:
${#name[subscript]} expands to the length of ${name[subscript]}.

Quote:
Originally Posted by lutusp View Post
...
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"?
 
Old 09-16-2009, 06:07 AM   #7
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,049

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
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.
 
Old 09-16-2009, 08:53 AM   #8
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,758

Rep: Reputation: 644Reputation: 644Reputation: 644Reputation: 644Reputation: 644Reputation: 644
true.. my mistake
 
Old 09-16-2009, 12:49 PM   #9
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 101Reputation: 101
Quote:
Originally Posted by archtoad6 View Post
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.
 
Old 09-17-2009, 03:45 AM   #10
abharsair
LQ Newbie
 
Registered: Jan 2009
Posts: 17

Original Poster
Rep: Reputation: 0
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
 
Old 09-17-2009, 08:22 AM   #11
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,049

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
recover individual files concatenated with gzip dandini Linux - Software 7 09-04-2011 03:12 PM
LXer: This week at LWN: Fun with NULL pointers, part 2 LXer Syndicated Linux News 0 08-01-2009 03:51 PM
LXer: This week at LWN: Fun with NULL pointers, part 1 LXer Syndicated Linux News 0 07-30-2009 09:42 PM
Validating concatenated cin inputs (C++) Swakoo Programming 5 05-17-2007 10:42 PM
LXer: Desktop FreeBSD Part 3: Adding Software LXer Syndicated Linux News 0 04-13-2007 08:16 AM


All times are GMT -5. The time now is 12:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration