Search column and store inside variable using grep or awk

You may use this generic solution using awk:

cat srch.awk
BEGIN{ FS = "[[:blank:]]*[|]" }
{
   for (i=1; i<=NF; ++i) {
      if (match($i, /^[_[:alnum:]]+: /) && substr($i, 1, RLENGTH-2) == fld) {
          print ( substr($i, RLENGTH+1) )
          next
      }
   }
}

Then use it as:

awk -v fld='isp' -f srch.awk file.txt

OVH Hosting Inc.
Teredo RFC4380
Teredo RFC4380
Teredo RFC4380
Teredo RFC4380
Teredo RFC4380
Teredo RFC4380

Or else:

awk -v fld='usage_type' -f srch.awk file.txt

Data Center/Web Hosting/Transit
Reserved
Search Engine Spider
Hacking
Gaming
IPS
Commercial

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top