简体   繁体   中英

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

$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

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 ' {
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] ),
  • Third character in $5 is 1 or 2 ( [1-2] )
  • $11 only can be 229 or 49

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.

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