Atul D
Atul D

Reputation: 29

awk those value contain blank

how can I grep those values which are having space in it. I' am trying :

awk -F '\t' '$19 ~ /[0-9[:blank:]0-9]/ {print $19}' myfile.txt
0000280000
0000209600
0000181 96
0000179586
0000122393
0000000000

Upvotes: 0

Views: 401

Answers (3)

hek2mgl
hek2mgl

Reputation: 158010

First, the expression [0-9[:blank:]0-9] matches the numbers 0-9 or a blank. (the second 0-9 is superfluous). This is true for all of your test data in column 19, that's why they all got printed.

If you want to check for a blank - which is a space or a tab character - you can just use:

awk -F'\t' '$19 ~ /[[:blank:]]/{print $19}'

however, since fields are delimited by tab, you can just use a literal space:

awk -F'\t' '$19 ~ / /{print $19}'

Besides that, checking for a literal space in a field doesn't require a regex in awk, you can use index():

awk -F'\t' 'index($19, " "){print $19}' myfile.txt

index() returns zero if field 19 doesn't contain a space, otherwise the position of the first occurrence in the string. Since string indexes start at 1 in awk this position will be always greater than zero which evaluates to true and makes awk print that field in the following block.

Upvotes: 2

Lohit Gupta
Lohit Gupta

Reputation: 1081

You can do this by using a simple grep statement:

grep ' ' filename.txt

For finding space in a particular field, use this:

cat file.txt|awk -F '\t' {'print $19'}|grep ' '

Upvotes: 0

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

You should specify POSIX character class separately, like below:

awk -F '\t' '$19~/[0-9]+[[:space:]]/{ print $19 }' myfile.txt

Upvotes: 0

Related Questions