Identify empty file with null record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Identify empty file with null record
# 1  
Old 06-29-2016
Identify empty file with null record

Hi Team,

I have a file abc.dat which is a empty file. But it has null record in first line. I need to identify this unique file and handle it separately.


scenario 1:
Code:
abc/dw> wc abc.dat
1 0 1 abc.dat
abc/dw> cat abc.dat

abc/dw>

scenario 2:
Code:
abc/dw> wc pqr.dat
0 0 0 pqr.dat
abc/dw> cat pqr.dat

abc/dw>

scenario 3:
Code:
abc/dw> wc xyz.dat
2 2 2 xyz.dat
abc/dw> cat xyz.dat
adf 
adsf
abc/dw>

If the file has null record in it
then echo "empty file with null record" -- abc.dat
else echo "empty file" -- pqr.dat
else echo "file has data in it" -- xyz.dat

Can anyone help me out?

Last edited by RudiC; 06-30-2016 at 05:58 AM.. Reason: Changed ICODE to CODE tags.
# 2  
Old 06-29-2016
The following script assumes that /bin/sh is a Posix-compatible shell.
If the raw read is successful then it has got a record, then the record is tested for being zero (empty, null).
Code:
#!/bin/sh
if IFS= read -r line < "$1"
then
  if [ -z "$line" ]
  then
    echo "file with null record"
  else
    echo "file has data in it"
  fi
else
  echo "empty file"
fi

The script takes a file name as argument, otherwise reads from stdin.
For academic interest, the following works identically because the if ... fi is a block where input and output can be redirected.
The difference is, that the input file is open throughout the block, so another read in the block would read the next line.
Code:
#!/bin/sh
if IFS= read -r line
then
  if [ -z "$line" ]
  then
    echo "file with null record"
  else
    echo "file has data in it"
  fi
else
  echo "empty file"
fi < "$1"


Last edited by MadeInGermany; 06-29-2016 at 04:56 PM..
# 3  
Old 06-30-2016
How about
Code:
case "$(wc -c <file)" in 0) echo empty file;; 1) echo file with null record;;  *) echo file with data;; esac

It would work on the given examples, but fail if a (non-*nix-text) file with a single character but without line terminator were encountered.
This User Gave Thanks to RudiC For This Post:
# 4  
Old 07-01-2016
wc has two problems.
  1. wc goes through the entire file. Can take a long time.
  2. works only with GNU wc. A Unix wc puts leading spaces (BTW a frequent obstacle); the case does not match.
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Parsing null or empty output

I am working an some if then statements for a script. I want to be able to check for alpha characters or empty out put then exit out. if ]]; echo "Serial Number Invaild" then exit 3; How do I account if the output is empty or null in this in this statement. Many thanks (6 Replies)
Discussion started by: andysensible
6 Replies

2. Shell Programming and Scripting

To find record having null value

Hi All My requirement is to find the null values in particular column of a file and reject it in case if it contains null values. But the challenge is that I want a common command which can be used across different file, as the position of the column we need to check for different file may get... (14 Replies)
Discussion started by: ginrkf
14 Replies

3. Shell Programming and Scripting

awk to identify empty fields in line

I am trying to use awk to identify and print out records in fields that are empty along with which line they are in. I hope the awk below is close, it runs but nothing results. Thank you :). awk awk -F'\t' 'FNR==NR ~ /^*$/ { print "NR is empty" }' file file 123 GOOD ID 45... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

Identify Trailer record

Hello, My script has a trailer record(TR). Now I need to implement a logic, if TR is missed on the file it should generate me an email stating TR was not on the file.. Kindly help. (15 Replies)
Discussion started by: Harimalyala
15 Replies

5. Shell Programming and Scripting

how to add empty filed to record

hi i have record looks like below 1,US I want to add empty field to the record as below 1, , , ,US how i can do it using awk ? i tried with awk its not working awk '{ print $1", ,"$2 }' filename > file 1 (2 Replies)
Discussion started by: raghavendra.cse
2 Replies

6. Shell Programming and Scripting

PHP: how can I delete empty/NULL elements from a multi-dimensional array.

Hi all I have a file that i'm running and exec(cat ./dat) against..and putting its contents into any array, then doing an exploding the array into a multi-dimension array... The 15 multi-dimensional arrays have elements that are null/empty, I would like to remove/unset these elements and then... (2 Replies)
Discussion started by: zeekblack
2 Replies

7. Shell Programming and Scripting

Table null is empty

hi I m executing a shell script in which records are to be fetched from a table. If the sql that is running empty result set it is displaying "table null is empty !". I dont want this msg on the standard output. COuld you please help me in this regard (3 Replies)
Discussion started by: priyanka3006
3 Replies

8. Shell Programming and Scripting

How to check for null or empty string

Hi, I need to check for value not equal (<>) to 21 and not equal empty or null values. Please modify this script if then echo "$VALUE,$BSC_NAME,$BSC_ID" > $OUT_FILE/power_up.out end if TQ (5 Replies)
Discussion started by: doer
5 Replies

9. Shell Programming and Scripting

identify the empty directories

Hi Wrote the below script to identify the empty directories ,when executing the below showing that directory is not empty but the directories are empty.Please help to identify the empty directories 33 is not empty 33 is not empty 33 is not empty 33 is not empty for file in `find .... (5 Replies)
Discussion started by: mohan705
5 Replies

10. Shell Programming and Scripting

If statement falling over on a null record. Help please.

Okay i've got some code which reads a text file and loops through it and there a few if statements inside where one is failing (the one bolded). Basically the acc column contains a list of three digit access codes, some though have null records (i.e nothing in the field) so what I want to do is... (3 Replies)
Discussion started by: TonyR
3 Replies
Login or Register to Ask a Question