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.
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
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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.