tags:

views:

41

answers:

3

I want to extract the variable # of digits between two patterns, eg:

correction: blah blah.... AAM #6,blah blah

blah blah.... AAM #10 , blah blah

blah blah.... AAM #100 , blah blah

output: 6, 10 and 100

I need to extract numbers between "AMA #" and comma

A: 

try a regular expression like (?<=#)\d+

Marcelo de Aguiar
A: 

Assuming the two patterns the digits are supposed to be between are AAM# and ,

gawk 'match($0, /AAM #([[:digit:]]+)[[:space:]]*,/, a) {print a[1]}'

glenn jackman
This works well. Thanks!!
aapl
I was trying to use this as a part of a larger search and am encountring errors. The cmd is sed -n "/COMPLETE/p" 1.txt | gawk 'BEGIN { FS = "/" } {printf"%s %s:%s \n", substr($4,1,10),substr($6,8,1), match($7, /AAM #([[:digit:]]+)[[:space:]]?,/, a) a[1] }' I am trying to get the digit into 3rd %s of printf and I see that the number is preceeded by a count (2 or 3). Any idea what my mistake is?
aapl
A: 

No need to use sed if you use awk. Its redundant.

gawk 'BEGIN{FS="/"}/COMPLETED/{
  match($7, /AAM #([[:digit:]]+)[[:space:]]?,/, a)
  printf "%s %s:%s \n", substr($4,1,10),substr($6,8,1),  a[1] 
} ' file
ghostdog74
Even better. It has been 15 yrs since I scripted. Tried the above and there is a syntax error at printf. Added {} like so and this gives incorrect results. gawk 'BEGIN{FS="/"}/COMPLETED/ { match($7, /AAM #([[:digit:]]+)[[:space:]]?,/, a) } {printf "%s %s:%s \n", substr($4,1,10),substr($6,8,1), a[1] } ' file
aapl