Hi Experts.
I'm stuck with the below AWK code where i'm trying to move the records containing any special characters in the last field to a bad file.
awk -F, '{if ($NF ~ /^[0-9]|^[A-Za-z]/) print >"goodfile";else print >"badfile"}' filename
sample data
1,abc,def,1234,A *
2,bed,dec,342,* A
3,dec,345,23,*&^
4,sdf,fgh,234,
5,ert,345,ghj,C**
6,ert,345,sdf,123 ---- only valid record
The output required must contain the first 5 records in badfile and the last record in good file.
But my above awk logic cosiders only the below records as badfile records:
2,bed,dec,342,* A
3,dec,345,23,*&^
4,sdf,fgh,234,
The other two invalid records ("A " and "C*") are being written into goodfile which is wrong.
Please help me fix this.
Note: the $NF values can contain [spaces:] between any alphanumeric chars. However, all spaces or null is considered a bad record.
Thanks Gurus!