2

Is there any way to cat a file that has something like:

field1,field2,field number 3,field4,field5

field1,field2,field3,field4,field5

(Some fields have spaces, some fields do not)

I want to print using awk $1 and $3, but only lines that do not have a space in field 3.
I don't know if there is any way to make awk print something only if it starts with , and ends with ,

  • 1
    Do any of the existing answers solve your problem? Please consider Accepting it, if so, using the checkmark next to it. Thank you! – Jeff Schaller Apr 12 '17 at 1:35
9
awk -F, '$3 !~ / / {print $1,$3}' input-file
3

Lines don't have the space in Field 3

awk -F, '{n=split($3,a," ");if(n==1){print $1,$3}}' filename
2
perl -F, -lane '$F[2] =~ /\s/ || print "@F[0,2]"' yourfile

sed -ne 's/^\([^,]*\),[^,]*,\([^ ,]*\)\(,.*\)\{0,\}$/\1 \2/p' yourfile

while IFS=, read -r f1 x f3 x; do case $f3 in *" "* ) : ;; * ) echo "$f1 $f3" ;; esac; done < yourfile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.