I have a comma delimited file that I need to validate the data in one two columns in.
Sample File:
1234,1234,1234,DESCRIPTION,1,1,2
1234,1234,1234,DESCRIPTION,1,1,2
1234,1234,1234,DESCRIPTION,1,1,2
1234,1234,1234,DESCRIPTION,1,1,2
I need to make sure that the second column's entries are numeric. If they have an alpha character, I need to delete that line of data and write it to another file.
I'm not sure where to get started, and any advice would be appreciated.
Thanks!
kshji
July 15, 2009, 12:04pm
2
#!/bin/ksh
validate()
{
val="$2"
# remove all numbers = some left = not numbers
# new=$( echo "$val" | tr -d "[0-9]" )
# without external command:
new=${val//[0-9]/}
[ "$new" != "" ] && echo "$*" >&2 && return 1
echo "$*"
}
oifs="$IFS"
while read line
do
IFS=","
validate $line
IFS="$oifs"
done
And use this script:
cat file | ./thisscript > ok.txt 2>err.txt
You can try alaso awk solution:
awk -v FS=',' -v OFS=',' '{
if ($2 ~ /^[0-9]*$/)
print $0
else
print $0 >> "err_file"
}' input_file
try :
sed -n '/.*,[0-9]*,.*,.*,.*,.*,.*/p' filename