Solved

Check if Data file zero length and Count File contains all zeros

Posted on 2007-03-25
7
1,570 Views
Last Modified: 2013-12-26
Hi,

I am writing a script in korn shell and need to do the following
1. Check if the incoming DATA file is of zero length
2. and if the DATA file is zero length check if the COUNT file is either empty or contains all zeros as nos

Hence
if [ -s DATAfilename ] and [ Count File contains nothing (empty) OR Count File contains all zeros]

then
  something
else
  something else
An actual example would be useful.
0
Comment
Question by:hcdev
  • 4
  • 3
7 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 18789439
This is a little long winded, but will do what you require.

#!/bin/ksh
zero=`grep -v 0 countfile`

if [[ -f datafile && ! -s datafile ]] && [[ -f countfile && ! -s countfile || -z "$zero" ]]
then
   echo true
else
   echo false
fi
0
 

Author Comment

by:hcdev
ID: 18794850
Hi Tintin,

Thanks for the reply. However I had 2 questions

1. The count file must contain all zeros. For example the count file can contain '000119' in this case the count file does not contain all zeros and hence the result should be a failure (else step). It should succeed only if the count file contains all zeros eg '000000'

OR

2. The count file is empty then succeed
0
 
LVL 48

Expert Comment

by:Tintin
ID: 18796535
Assuming count file only contains numbers, then change

zero=`grep -v 0 countfile`

to

zero=`grep  0 countfile | grep "[1-9]"`
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:hcdev
ID: 18796673
Hi Tintin

I responded to your earlier solution too. The problem here is with

[[ -f countfile && ! -s countfile || -z "$zero" ]]

Again what I need is to

1. Check if count file is not empty then it contains all zero's so for eg "0001119" is not valid but "0000000" is valid. Hence the only nos must be only zero's
OR
2. The count file is empty i.e. contains nothing'

All this with a zero byte data file

Your solution greps the file for any occurence of zero and then checks if the $zero is an empty string.
It does not provide me with what I am looking for. I tried it but does not work.

Thanks
0
 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
ID: 18796779
My script works according to the information given, eg:

$ cat script.ksh
#!/bin/ksh
zero=`grep 0 countfile | egrep "[1-9]"`

if [[ -f datafile && ! -s datafile ]] && [[ -f countfile && ! -s countfile || -z "$zero" ]]
then
   echo true
else
   echo false
fi

$ ls -l countfile datafile
-rw-r--r--   1 tintin   tintin        8 Mar 27 11:45 countfile
-rw-r--r--   1 tintin   tintin        0 Mar 26 08:39 datafile

$ cat countfile
00000

$ ./script.ksh
true

$ echo 0001119 >countfile
$ ./script.ksh
false
0
 

Author Comment

by:hcdev
ID: 18796820
Hi Tintin,

The reason it was failing was because the count file necessarily does not contain a zero with a positive number [1-9] Hence I had count files with values such as 121 which did not have a zero and hence it would evaluate to true. But I figured it out. I shall still accept your answer. Thanks for all your help
0
 
LVL 48

Expert Comment

by:Tintin
ID: 18796860
In that case, if you change

zero=`grep 0 countfile | egrep "[1-9]"`

to

zero=`egrep "[0-9]" countfile | egrep "[1-9]"`
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now