Graham
Graham

Reputation: 33

Formatting with awk

Bit of a beginner with awk and was hoping someone could point out where i'm going wrong..

I'm trying to run awk in a script and change the formatting of particular strings matching "objectID"

This is the source data:

name=SDC1NM519
  capacityInKB=1,341,231,104
  osType=Windows
      objectID=LU.R700.53280.24580
      displayName=00:60:04
      capacityInKB=1,048,576
      consumedCapacityInKB=43,008
      dpPoolID=10
      objectID=LU.R700.53280.24584
      displayName=00:60:08
      capacityInKB=1,335,885,824
      consumedCapacityInKB=375,588,864
      dpPoolID=10

and this is my awk:

awk '/name/
/osType/
/objectID=LU {print "objectID=" substr ($1,18,5) }/ 
/displayName/
/capacityInKB/
/consumedCapacityInKB/
/dpPoolID/' rawdata.txt >> objtxt

What i'd like to see is something like below with the object ID changed to show only the "53280". Yes the number is always in the same format but different numbers:

name=SDC1NM519
  capacityInKB=1,341,231,104
  osType=Windows
      objectID=53280
      displayName=00:60:04
      capacityInKB=1,048,576
      consumedCapacityInKB=43,008
      dpPoolID=10

What i'm getting is this... no object at all...??

name=SDC1NM519
  capacityInKB=1,341,231,104
  osType=Windows
      displayName=00:60:04
      capacityInKB=1,048,576
      consumedCapacityInKB=43,008
      dpPoolID=10

Anyone able to help??

Upvotes: 1

Views: 54

Answers (2)

Kent
Kent

Reputation: 195209

if you just want the objectId line to show up, change your:

/objectID=LU {print "objectID=" substr ($1,18,5) }/

into

/objectID=LU/ {print "objectID=" substr ($1,18,5) }

However this will break the format (indentation is missing).

To keep the indentation, you can do :

/objectID=LU/&&sub(/=.*/,"="substr($1,18,5))

Upvotes: 1

glenn jackman
glenn jackman

Reputation: 247042

You're not seeing the objectID line because awk can't find a match

/objectID=LU {print "objectID=" substr ($1,18,5) }/

slashes delimit just the regular expression, not the whole line. Use this

/objectID=LU/ {print "objectID=" substr ($1,18,5) }

or

/objectID=LU/ {split($0, a, "."); print "objectID=" a[3]}

An awk program consists of a list of expression {action} pairs.

  • if the expression is absent, by default it is "true"
  • if the {action} is absent, by default it is {print}
  • if the expression evaluates to "true", the action is performed

Upvotes: 1

Related Questions