File with data

My requirement is to find out data in any of the file in particular path.

I am using UNIX OS.

For example I have two files. A123.rej & A345.rej

A123.rej contain data

123,456

A345.rej is a empty file.

I tried a code to find , if any of this file contains data, I need to echo as data found, if both the files are empty, then I will echo as No data. Currently I am using the below code.But its not working.Kindly help on this

if [ -s A*.rej ]
then echo "Data"
else
echo "No data"
fi
 

Is this a homework assignment? If not, how will this information be used?

Which UNIX OS are you using?

What shell are you using?

Are there always exactly two files? Are they always named A123.rej & A345.rej ?

In what way does your code "not work"? What diagnostics does it produce? When all of your files are empty, what output does it produce? When one or more of your files contain data, what output does it produce? Does it behave differently if there is only one file with a name ending in .rej ?

hi

Please find my answers below inline

Is this a homework assignment? If not, how will this information be used?

--> This is part of my project work.Actually I am writing a job , where we can call unix commands also.So my requirement is if I get some n number of input files, I will create same number of reject files with the format sourcefilename.rej. Source file name is in the below format

Party_YYYYMMDD_HHMISSS.txt

so if I have two source files I will have two reject files.

Which UNIX OS are you using?-->
What shell are you using?--> Regarding this I am not sure, as this is not my main tool.Very rarely I use UNIX.

Are there always exactly two files? Are they always named A123.rej & A345.rej? --> Hope the above explanation will answer this question

In what way does your code "not work"? What diagnostics does it produce? When all of your files are empty, what output does it produce? When one or more of your files contain data, what output does it produce? Does it behave differently if there is only one file with a name ending in .rej? --->

I have two rej files like this

Party_20120101_121212.rej
Party_20120101_121214.rej

When I tries to execute the below code I am getting some error.

if [ -s Party*.rej ]
> then
> echo "Data"
> else
> echo "No DATA"
> fi
-bash: [: Party_20120101_121212.rej: binary operator expected
No DATA

My expectation is that, if we have n number of Party*.rej files and if any of the file contains data in it(Data means there should be more than two records in the file, as the row is the header), I need to get echo as "Data"

I am not sure whether this convey my requirement or not. To be frank, I am not very good in UNIX :slight_smile:

That specification still is not that easy to understand. "No Data" seems not to be equivalent to "empty file", so the -s primary in the conditional expression can't be used. Aside: -s requires one single argument, not a list of files as probably produced with the wild cards patterns if several matches exist.

Based on your before last paragraph, would this come close to what you need:

[ $(cat *.rej | wc -l) -gt $(ls -1 *.rej | wc -l) ] && echo "Data" || echo "No data"
1 Like

You could also try the following:-

[ $(wc -l A*.rej | tail -1) = "0 total" ] && echo "No data" || echo "Data"

...... or for each individual file:-

for file in A*.rej
do
   [ -s "$file" ] && state="no data" || state="data"
   printf "The file %s has %s.\n" "$file" "$state"
done

Both of these will fail, however, if the number of matched files is large and causes the executed command to be too big. if this is likely, then you may need to go with a find command something like this:-

[ $(find . -type f | xargs cat | wc -l) -eq 0 ] && echo "No data" || echo "Data"

Would any of these help?

Robin

1 Like

This will work :-

found=0
if [ -s A123.rej ]
then
  found=1
fi

if [ -s A345.rej ]
then
  found=1
fi

if [ $found = 0 ]
then
  echo "No Data"
else
  echo "Data found"
fi
1 Like

Multiple file testing like rbatte1 tried to tell, if you need only know if some while include something.

found=0
for f in A*.rej
do
    [ ! -s "$f" ] && continue
   found=1
   break
done
[ "$found" = 1 ] && echo "we have data in file $f"
1 Like

You could get the same output more simply with just:

for f in A*.rej
do
    [ ! -s "$f" ] && continue
   echo "we have data in file $f"
   break
done

or:

for f in A*.rej
do
    [ -s "$f" ] && echo "we have data in file $f" && break
done

as long as standard output is writeable.