I have a CSV file like
CK,ck
XYZ,xyz,xyzs
ABC,abc,abcs
PQR,pqr,pqrs
LMN,lmn,
IJK,ijk,
I have to check if something is written in column 3 except whitespace, then display the whole line.the output will be:
XYZ,xyz,xyzs
ABC,abc,abcs
PQR,pqr,pqrs
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityA sed
approach:
sed -n '/[^,]*,[^,]*,[^, ]\+/p' file
Or a grep
solution:
grep -oE '^[^,]*,[^,]*,[^, ]+' file
And awk
:
awk -F, '$3 ~ /[^, ]+/' file
Try:
$ awk -F, '$3 && $3 != " "' file
XYZ,xyz,xyzs
ABC,abc,abcs
PQR,pqr,pqrs
or:
$ awk -F, '$3 ~ /[^[:blank:]]/' file
You could do something like this in awk :
awk -F',' '{gsub(/[ \t]+$/,"",$3)}$3' foo.csv
This will remove trailing whitespaces and only print if there's something left in the third field.