Use to awk to match pattern, and print the pattern

Hi,

I know how to use awk to search some expressions like five consecutive numbers, [0-9][0-9][0-9][0-9][0-9], this is easy.
However, how do I make awk print the pattern that is been matched?

For example:
input: usa,canada99292,japan222,france59664,egypt223
output:99292,59664

Should work for you :rolleyes:

awk -F, '{for(i=0;++i<=NF;){gsub("[a-z]","",$i);if(length($i)==5){a=(a)?a FS$i:$i}}{print a;a=""}}' file

To keep the forums high quality for all users, please take the time to format your posts correctly.

  1. Use Code Tags when you post any code or data samples so others can easily read your code.
    You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags and by hand.)
  2. Avoid adding color or different fonts and font size to your posts.
    Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.
  3. Be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
Reply With Quote

Hi Dan,

Thanks a lot for your reply. But the code won't work in instances such as this:
input:

usa,66 canada99292,japan222,44 france59664,egypt223

because within each column, the field separator actually changes to space

Is it possible to have multiple field separator in place simultaneously, in this case: space and comma. If this is possible, then your code would work .

If your version of awk supports regex as RS, try this:

awk -v RS="[ ,]" -v ORS="," '/[0-9][0-9][0-9][0-9]/ {gsub(/[a-z]/,"");print}' file

With a slightly change of the code of danmero:

awk -F",| " '{for(i=0;++i<=NF;){gsub("[a-z]","",$i);if(length($i)==5){a=(a)?a "," $i:$i}}{print a;a=""}}'

if you have other options like grep or sed:

grep -o pattern file

or

sed -n '/pattern/ s/.*\(pattern\).*/\1/p' file

Yes but you should check the OP required output.