简体   繁体   English

awk选择列的一部分(3个字符中的第3个)

[英]awk select part of column (3rd of 3 characters)

I have a number of files where I select some columns with the following 我有很多文件,在其中选择以下几列

cat test.txt | awk ' {
if ($11 ~ /229/ && $5 ~ /1A1/) 
{print $0, ($3 + 1) }         
if ($11 ~ /229/ && $5 ~ /1A2/) 
{print $0, ($3 - 1) }
if ($11 ~ /49/ && $5 ~ /1A1/)  
{print $0, ($3 - 1)}
if ($11 ~ /49/ && $5 ~ /1A2/)
{print $0, ($3 + 1) }
}' > output

The problem is that some files might have different letter for 1A1 or 1A2 such as 1K1 , 1K2 ..... I would like to make the condition $5 ~(/1A2/) or $5 ~(/1A1/) more "general" and I tried things like 问题是,有些文件可能有不同的字母1A11A21K11K2 .....我想提出的条件$5 ~(/1A2/) or $5 ~(/1A1/)更多的“一般”我尝试了类似的事情

$5 ~(/??1/)
$5 ~(/??2/)

So far no success. 到目前为止没有成功。 Can anyone help me on that? 有人可以帮我吗? Please note that I need to keep also the selection criteria in $11 请注意,我还需要将选择标准保持在11美元

cat test.txt | awk ' {
    if ($11 ~ /229/ && $5 ~ /??1/)
....

You should try with regular expressions, in this case you can use this simple awk program: 您应该尝试使用正则表达式,在这种情况下,您可以使用以下简单的awk程序:

awk ' {
if ($11 ~ /(229|49)/ && $5 ~ /1[A-Z][1-2]/) 
{print $0, ($3 + 1) }}' test.txt > output

I'm assuming that: 我假设:

  • Second character in $5 is any Capital/Mayus Letter( [AZ] ), $5第二个字符是大写字母或马约斯字母( [AZ] ),
  • Third character in $5 is 1 or 2 ( [1-2] ) $5第三个字符是12[1-2]
  • $11 only can be 229 or 49 $11只能是22949

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM