Home > Software engineering >  How to match a unique patter using awk?
How to match a unique patter using awk?

Time:10-01

I have a text file called 'file.txt' with the content like,

test:one
test_test:two
test_test_test:three

If the pattern is test, then the expected output should be one and similarly for the other two lines.

This is what I have tried.

pattern=test && awk '{split($0,i,":"); if (i[1] ~ /'"$pattern"'$/) print i[2]}'

This command gives the output like,

one
two
three

and pattern=test_test && awk '{split($0,i,":"); if (i[1] ~ /'"$pattern"'$/) print i[2]}'

two
three

How can I match the unique pattern being "test" for "test" and not for "test_test" and so on.

CodePudding user response:

If you want to use a regex, you can create it dynamically with pattern and optionally repeating _ followed by pattern until matching a :

If it matches the start of the string, then you can print the second field.

awk -v pattern='test' -F: '
$0 ~ "^"pattern"(_"pattern")*:" {
    print $2
}
' file

Output

one
two
three

Or if only matching the part before the first underscore is also ok, then splitting field 1 on _ and printing field 2:

awk -v pattern='test' -F: ' {
  split($1, a, "_")
  if(a[1] == pattern) print $2
}' file

CodePudding user response:

How can I match the unique pattern being test for test and not for test_test and so on.

Don't use a regex for comparing the value, just use equality:

awk -F: -v pat='test' '$1 == pat {print $2}' file
one

awk -F: -v pat='test_test' '$1 == pat {print $2}' file
two

If you really want to use regex, then use it like this with anchors:

awk -F: -v pat='test' '$1 ~ "^" pat "$" {print $2}' file

one

CodePudding user response:

Using GNU sed with word boundaries

$ sed -n '/\<test\>/s/[^:]*://p' input_file
one
  • Related