Operating System - HP-UX
1842315 Members
2824 Online
110188 Solutions
New Discussion

Re: script or command to check for delimiters

 
SOLVED
Go to solution
MAD_2
Super Advisor

script or command to check for delimiters


I have a few hundred rows of data that had to be scrubbed and I now need to check the data integrity of all rows by making sure all of the fields are present in each.

The delimiter used was pipes "|" and there should be a total of 14 en each row. How can I check that for each row there are a total of 14 pipes and to show me those rows that do not have the 14 pipes?

Thanks!
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with
3 REPLIES 3
curt larson_1
Honored Contributor

Re: script or command to check for delimiters

cat file |
awk -F"\|" '{ if ( NF != 15 )
printf("line %d, %s\n",NR,$0);}'
curt larson_1
Honored Contributor
Solution

Re: script or command to check for delimiters

and for a little more information

cat file |
awk -F"\|" '{ if ( NF != 15 ){
if ( NF < 2 ) delim=0;
else delim=NF--;
printf("line %d, delimiters = %d, %s\n",NR,delim,$0);}
}'
john korterman
Honored Contributor

Re: script or command to check for delimiters

Hi,
a traditional, slow approach. Try running it with $1 as input file:

#!/usr/bin/sh
# Check CORRECT num of delim in $1

typeset -i NUM_CHARS=0 AFTER_DEL=0 DIFF=0 LINE_NO=1 CORRECT=14
typeset DELIM="\|"
echo "Line_no\tDelim_no\tLine"
while read line
do
NUM_CHARS=$(echo "$line"| wc -c)
AFTER_DEL=$(echo "$line" | tr -d $DELIM |wc -c)
DIFF=$(( $NUM_CHARS - $AFTER_DEL ))
if [ "$DIFF" != "$CORRECT" ]
then
echo "$LINE_NO\t$DIFF\t$line"
fi
LINE_NO=$(( $LINE_NO + 1))
done <$1

regards,
John K.
it would be nice if you always got a second chance