Discussion Locked

search for blank fields

0 helpful
7 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I have a file with records subdivided by semicolons into fields. I want to show the records with at least one field each completely blank. I have tried grep "[;[:blank:];]"

7 replies

by gary_w

Here's one of the umpteen ways you could do it. I'm sure someone will come up with a cool way to dump out lines, but I wrote this script to process each line. What you give up in performance you gain back in flexibility. For example, you could dump the lines with the blank columns into a separate file for error processing, or write column values to another file, setting the blanks with a pre-determined value, etc.

input file, in.txt:
1;2;3;4;5;6;7;8;9
2; ;3;4;5;6;7;8;9
3;2;3;4;5;6;7;8;9
4;2;3;4; ;6;7;8;9
5;2; ;4;5;6;7;8;9

shell script, filescan.sh:
##
## filescan - Prints lines with a null column.
##
## USAGE: filescan
##
export col_ctr=1
export EXPECTED_COLS=9

# Loop through all lines in the file
cat $1 | while read line
do
# Loop through columns in the line
while :
do
colval=`echo $line|cut -f${col_ctr} -d\;`
# If a column is null...
if [ -z $colval ]
# and it's not the end of the line...
then if [ $col_ctr -le $EXPECTED_COLS ]
then echo "$line" # print it per requirements
# Could echo it to a file instead?
break
else break # End of the line
fi
fi
col_ctr=`expr $col_ctr + 1` # increment column counter
done
col_ctr=1 # Reset column counter
done
# End filescan.sh

Command line:
filescan.sh in.txt

Gary

by gary_w

You should add a line counter too, so when a line prints the line number would be included in case you need to go edit the source file or whatever.

Ok, back to work.
Gary

by john.a.wills

I am looking for blank fields, not empty ones. There does not appear to be a string comparison option for blanks, and I do not think that = ' ' will work for more than one blank. Thank you for the interesting piece of code.

by gary_w

There ARE blank characters. I even used variying numbers of blanks in each row. They didn't show up right due to the formatting. Why don't you try it and see first?

Gary

by john.a.wills

I have run it. I had to put quotation marks around colval where it gets tested (perhaps because I am not in Linux but Unix). I get a line output for a field only partly blank.

This discussion is closed to new replies.