×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

awk - transposing output of program

awk - transposing output of program

awk - transposing output of program

(OP)
hello awk gurus;

I have the following problem which I am having difficulties with.

I have a file that is produced on a hourly basis which I would like to friendly format so that it is easier to interpret. Currently, the contents of file is presented on four lines:

header: startdate and time and some other fields
physical-memory: data
virtual-memory: data
footer : enddate and time

Output is colon (:) delimited.

example:

eport-high:header:20211223T010100Z:20211223T020100Z:10:7200
report-high:physical-memory:mem_default:[total]:97124608K:72.36%:-:-
report-high:virtual-memory:vm_default:[total]:102675472K:61.19%:-:-
report-high:footer:20211223T020100Z10:7200

Data is produced in groups of four rows with header and footer containing start and end time. I would like to display each group of four lines as a single line in a new file so it looks like below (comma delimited and transposed):

Startdate,time,Enddate,time,%physicalmemory,physicalmemory,%virtualmemory,virtualmemory;
20211223,T010100Z,20211223,T020100Z10,72.36%,97124608K,61.19%,102675472K;


How do i do this with awk or gawk in solaris ?

Any help appreciated.

Thanks in advance!
dean

RE: awk - transposing output of program

Hi dlicheri,

Something like this should do the work:
dlicheri.awk

CODE

# run:
# awk -f dlicheri.awk dlicheri.txt

BEGIN {
  FS = ":"
}

$2 ~ /header/ {
  startdate = substr($3, 1, 8)
  starttime = substr($3, 9, 8)
}

$2 ~ /physical-memory/ {
  physicalmemory_total = $5
  physicalmemory_proc = $6
}

$2 ~ /virtual-memory/ {
  virtualmemory_total = $5
  virtualmemory_proc = $6
}

$2 ~ /footer/ {
  enddate = substr($3, 1, 8)
  endtime = substr($3, 9, 8)
}

END {
  printf "%s,%s,%s,%s,%s,%s,%s,%s\n",
    "startdate", "starttime", "eddate", "endtime", 
    "%physicalmemory", "physicalmemory", "%virtualmemory", "virtualmemory"
  printf "%s,%s,%s,%s,%s,%s,%s,%s\n",
    startdate, starttime, enddate, endtime, 
    physicalmemory_proc, physicalmemory_total, virtualmemory_proc, virtualmemory_total
} 

Now when we have your input file
dlicheri.txt

CODE

report-high:header:20211223T010100Z:20211223T020100Z:10:7200
report-high:physical-memory:mem_default:[total]:97124608K:72.36%:-:-
report-high:virtual-memory:vm_default:[total]:102675472K:61.19%:-:-
report-high:footer:20211223T020100Z10:7200 

after running this command

CODE

$ awk -f dlicheri.awk dlicheri.txt > dlicheri_output.txt 

we get this output file
dlicheri_output.txt

CODE

startdate,starttime,eddate,endtime,%physicalmemory,physicalmemory,%virtualmemory,virtualmemory
20211223,T010100Z,20211223,T020100Z,72.36%,97124608K,61.19%,102675472K 

I don't have Solaris, I tried it on Linux.

RE: awk - transposing output of program

(OP)
hello mikrom;

that's great, it works. If i wanted to pipe the output directly to the awk script what would need to change ?

eg:
# collect-stats | dlicheri.awk >> /var/tmp/report-today

RE: awk - transposing output of program

Quote (dlicheri)


If i wanted to pipe the output directly to the awk script what would need to change ?
this works for me:

CODE

$ cat dlicheri.txt | awk -f dlicheri.awk > dlicheri_output.txt 

RE: awk - transposing output of program

Hi blicheri,

or, if you insert on the 1st line of the script the awk shebang, i.e. depending on the location of awk, e.g.
in case of

CODE

$ which awk
/usr/bin/awk 
you set this shebang

CODE

#!/usr/bin/awk -f
...
the awk script
... 
and then change mode of the script to executable

CODE

$ chmod +x dlicheri.awk 
then you will be able to call that like this:

CODE

$ cat dlicheri.txt | ./dlicheri.awk > dlicheri_output.txt 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close