1

I have a file contain following output.

ora.abc.db
      1        ONLINE  ONLINE       serverA              Open
      2        ONLINE  ONLINE       serverB              Open
ora.xyz.db
      1        ONLINE  ONLINE       serverA              Open
      2        ONLINE  ONLINE       serverB              Open
      2        ONLINE  ONLINE       serverC              Open

I want to format this file in following way, but i want to use shell script only, I believe we can use awk but i don't have logic

abc  abc1  ONLINE serverA
abc  abc2  ONLINE serverB
xyz  xyz1  ONLINE serverA
xyz  xyz2  ONLINE serverB
xyz  xyz3  ONLINE serverC
5
  • 2
    What is the problem? Write the script.
    – ceving
    Sep 20, 2013 at 19:03
  • I suspect there is a typo, and you want the third line in ora.xyz.db to begin with the number 3, right?
    – Floris
    Sep 20, 2013 at 19:03
  • 2
    How is one of the two ONLINE's in every line discarded?
    – konsolebox
    Sep 20, 2013 at 19:10
  • It is not clear if the word in the second column ("ONLINE") or the third column ("ONLINE") needs to be echoed. Nor whether anything should change in the output depending on whether the last column is "Open".
    – Floris
    Sep 20, 2013 at 19:33
  • ONLINE from second column field.
    – Satish
    Sep 20, 2013 at 19:47

2 Answers 2

5

This can be an aproach:

$ awk '/^ora/ {split($0,a,"."); next} {print a[2], a[2]$1, $2, $4}' a
abc abc1 ONLINE serverA
abc abc2 ONLINE serverB
xyz xyz1 ONLINE serverA
xyz xyz2 ONLINE serverB
xyz xyz2 ONLINE serverC

Explanation

  • /^ora/ applies for lines starting with ora. In those, split the content based on the dot . and store it in the array a. So it is a[1]=ora, a[2]=abc, ... Then next makes awk jump to next line.
  • print prints the 2nd field of the array a plus different elements from the line: $1 stating for the first, $4 stating for the 4th.
3
  • I am not AWK expert so could you please explain your syntax?
    – Satish
    Sep 20, 2013 at 20:04
  • @Satish sure, done! :)
    – fedorqui
    Sep 20, 2013 at 20:08
  • 1
    Freaking awesome logic, you rocks!!
    – Satish
    Sep 20, 2013 at 20:11
4

A slightly expanded (but largely equivalent) script:

{
if($0 ~ /^ora/)  {         # look for lines starting (^) with "ora"
    split($0, temp, ".");  # find the elements of the line an put in array
    serverName = temp[2];  # the "middle bit" - what was found between two dots
    next;                  # nothing else to do with this line
  }
else  {
    print serverName, serverName$1, $3, $4; # string together the bits
    # note that each $n corresponds to the nth "word"
    # by default fields are separated by white space
    # you can set the variable FS to something else (e.g. FS=",")
    # to deal with other formate
  }
}

Put this in a file server.awk, and if your input is in log.txt, invoke it with

awk -f server.awk log.txt

I hope the explanation adds some value...

1
  • Cool man!!!!!!!!!!!!!!!!!!!!!!!
    – Satish
    Sep 20, 2013 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.