General discussion

Locked

search for blank fields

By john.a.wills ·
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:];]" <remit01, but this yields records with fields only partly blank as well as wholly blank ones.

This conversation is currently closed to new comments.

7 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

by Gary_W In reply to search for blank fields

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 <file to process>
##
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

Collapse -

by Gary_W In reply to

ARGH formatting is lost. If you want, I'll
e-mail the files to you.
Gary

Collapse -

by Gary_W In reply to

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

Collapse -

by john.a.wills In reply to

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.

Collapse -

by Gary_W In reply to search for blank fields

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

Collapse -

by john.a.wills In reply to

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.

Collapse -

by john.a.wills In reply to search for blank fields

This question was closed by the author

Back to Linux Forum
7 total posts (Page 1 of 1)  

Related Discussions

Related Forums