ricknaght ricknaght - 1 year ago 48
Linux Question

Comparing string with various numbers in AWK

It's much easier to explain it with an example:

Say I have strings like these

stringhead034endstring 12
stringhead212endstring 32
stringhead234endstring 23

so they're all in the format

How could I manipulate and play around with them in a group regardless of the number they posses, say, store them in an array
? If I tried that, they all would be different.

If not sure if the question is clear, so I will try to elaborate.

I'm trying to store all those strings in one single array element, say myarray["mystring"], and manipulate the data next to them. I'm using something like this

{ myarray[$1] += ($1 == "mystring???endstring") * $2 } # Add the value if true

However, this would obviously not work as there are several different strings all differentiated by the number they have, but I wanna disregard that.

Desired output is something like
mystringendstring 67

Any idea?

Answer Source
$ cat ip.txt 
stringhead034endstring 12
stringhead212endstring 32
stringhead234endstring 23

$ awk '$1 ~ /stringhead.{3}endstring/{ sum += $2 } END {print "mystringendstring " sum}' ip.txt 
mystringendstring 67
  • $1 ~ /stringhead.{3}endstring/ match against first field. .{3} means any character three times. If needed, it can be changed to [0-9]+ to match one or more number of digits or [0-9]{3} for exactly three digits
  • Question mentions single element array, so I've taken liberty to use a simple variable instead and serves the purpose to get expected output as mentioned by OP