3

A csv file with multiple records is delimited by |.

field1|field2|field3|field4|field5

I want to check if field3 is blank or contains "space" characters only. If it is blank or space, the whole line should show up.

5 Answers 5

10
$ echo "1|2||4" | awk  -F'|' '$3 ~ /^[ \t]*$/   {print $0}'

1|2||4

$ echo "1|2|  |4" | awk  -F'|' '$3 ~ /^[ \t]*$/   {print $0}'

1|2|  |4

$ echo "1|2|  3|4" | awk  -F'|' '$3 ~ /^[ \t]*$/   {print $0}'
0
3

You can also use the cut command to pull out the third field and then test the value:

$ echo "field1|field2|field3|field4|field5" | cut -d '|' -f 3
field3
1
  • More readable than awk for this trivial situation if y' ask me (which nobody did ;))
    – Tom Newton
    Jul 9, 2009 at 7:08
1

My random attempt using grep would be:

grep -E '^[^|]*\|[^|]*\| *[^| ]+ *\|' file
2
  • The trouble with this approach is that it isn't general, so if you want to select on the 14th field it would become hateful.
    – James
    Jul 8, 2009 at 23:27
  • 1
    @James: Then try something like: "grep -E '^([^|]*\|){13} *[^| ]+ *\|' file" adjusting the value "13" appropriately
    – JMusgrove
    Jul 13, 2009 at 22:38
0

I'm not certain about unix, but in linux you would want to use the sed command.

sed 's/||/\n/g' will make it so that if there are any blank fields it will add a new line. not certain how to get it to only check the 3rd field. sed 's/| |/\n/g' should work for only spaces.

2
  • linux is a form of unix. Most things that are in linux are in other unixes. Jul 16, 2009 at 12:40
  • was this comment necessary? Sed, Awk, Grep and the like are SIMILAR across *NIX and BSD systems, but not always the same. Jul 23, 2009 at 19:53
0

Using Perl:

perl -F'\|' -lane 'print if $F[2] !~ /\S/' file

-a turns on autosplit mode, which splits the fields into array @F
-F'\|' sets the field delimiter to |
$F[2] is the 3rd field
!~ /\S/ tests for non-space characters (or empty)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .