Bash Script to filter data

Hi,
I need to clean data from a raw input file to output file.
I have to discard all rows where 4th column is empty. Column separator is ","

Input.txt
1,2,3,4,A,5,6,7,0
1,2,3,4,,5,6,7,0
1,2,3,4,B,5,6,7,0
1,2,3,4,,5,6,7,0
1,2,3,4,C,5,6,7,0
1,2,3,4,,5,6,7,0
1,2,3,4,D,5,6,7,0
1,2,3,4,,5,6,7,0
1,2,3,4,E,5,6,7,0

Output should be :
output.txt
1,2,3,4,A,5,6,7,0
1,2,3,4,B,5,6,7,0
1,2,3,4,C,5,6,7,0
1,2,3,4,D,5,6,7,0
1,2,3,4,E,5,6,7,0

Besides output I also need stats on filtered data
Count of Source Records  9
Count of Target Records  5
Count of skipped Records  4

Thanks
crazywolf2010Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

omarfaridCommented:
Try below

grep -v ',,' filename > filename.$$
s=`wc -l filename`
t=`wc -l filename.$$
echo count of source records $s
echo count of target records $t
echo count of skipped records `expr $s - $t`
echo results are stored in filename.$$
0
farzanjCommented:
By column 4, I think you really mean column 5 because 4th is where you have number 4 which is never missing.

The above solution would work if any column is empty

You can do:

grep -P '^[^,]*,[^,]*,[^,]*,[^,]*,[^,]+,' filename
0
woolmilkporcCommented:
Or awk:

awk -F"," '
    {if($5=="") S+=1;
        else {T+=1; print $0 > "output.txt"}}
    END {print "\nCount of Source Records", NR;
             print "Count of Target Records", T;
             print "Count of Skipped Records", S}' rawinput.txt
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.