genderbee
genderbee

Reputation: 213

Bash, grep lines from csv based on column 3, if length is 6 chars (numbers)

Question

I need in bash, grep lines from csv based on column 3 divided by ,, only if it consists of solely 6 digits.

Example

Input file.

,11,221951
,11,221952
,11,232928
,11,45
,11,4
,11,5
,11,6
,11,6

Output file (just lines based on 3th column, where are 6 chars).

,11,221951
,11,221952
,11,232928

I tried

cut -d, -f3 input_file.csv | grep -x '.\{6\}'

But it remove first two columns of course.

Thanks.

Upvotes: 2

Views: 691

Answers (3)

aborruso
aborruso

Reputation: 5698

Using Miller (https://github.com/johnkerl/miller) running

mlr --nidx --fs "," filter -S 'strlen($3)==6' input.csv

you will have

,11,221951
,11,221952
,11,232928

Upvotes: 0

RavinderSingh13
RavinderSingh13

Reputation: 133610

Could you please try following.

awk 'BEGIN{FS=","} length($3)==6'  Input_file

In case you want to make sure all should be digits then try:

awk 'BEGIN{FS=","} match($3,/^[0-9]{6}$/)'  Input_file

Upvotes: 2

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627082

You may use

awk -F',' '$3 ~ /^[0-9]{6}$/' file > newfile

Here,

  • -F',' sets the field separator to ,
  • '$3 ~ /^[0-9]{6}$/' keeps the lines where the third field value only consists of 6 digits.

See the online demo:

s=",11,221951
,11,221952
,11,232928
,11,45
,11,4
,11,5
,11,6
,11,6"

awk -F',' '$3 ~ /^[0-9]{6}$/' <<< "$s"

Output:

,11,221951
,11,221952
,11,232928

Upvotes: 2

Related Questions