首页 > 解决方案 > awk filter rows based on word range

问题描述

I have several files that contain millions of rows where the 6th field is a word (consisting of 3-5 letters). These words can start with any letter of the alphabet. I want to filter the output based on the letter the word starts with but the filter needs to go deep into the word, for example I want rows where the word starts with letter A through the letter CRZZZ, then I want rows where the word starts with CS through DMZZZ, etc.

I tried:

cat file | awk '$6 ~ /^[A-C]/'| head

This code returns rows based on the words' first letter but I need to go deeper into the word, so I tried:

cat file |  awk '$6 ~ /^[A-ANZZZ]/'| head

But this returns rows where the words starts with A, N or Z.

标签: awk

解决方案


听起来你可能想要这个:

awk '(("A" <= $6) && ($6 <= "CRZZZ")) || (("CS" <= $6) && ($6 <= "DMZZZ"))' file

但没有样本输入/输出,这是一个猜测


推荐阅读