Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk - transposing output of program 1

Status
Not open for further replies.

dlicheri

Systems Engineer
Dec 23, 2021
2
GB
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
 
Hi dlicheri,

Something like this should do the work:
dlicheri.awk
Code:
[COLOR=#0000ff]# run:[/color]
[COLOR=#0000ff]# awk -f dlicheri.awk dlicheri.txt[/color]

[COLOR=#6a5acd]BEGIN[/color] {
  [COLOR=#6a5acd]FS[/color] [COLOR=#6a5acd]=[/color] [COLOR=#ff00ff]":"[/color]
}

[COLOR=#6a5acd]$2[/color] [COLOR=#6a5acd]~[/color] [COLOR=#ff00ff]/header/[/color] {
  startdate [COLOR=#6a5acd]=[/color] [COLOR=#008b8b]substr[/color]([COLOR=#6a5acd]$3[/color][COLOR=#6a5acd],[/color] [COLOR=#ff00ff]1[/color][COLOR=#6a5acd],[/color] [COLOR=#ff00ff]8[/color])
  starttime [COLOR=#6a5acd]=[/color] [COLOR=#008b8b]substr[/color]([COLOR=#6a5acd]$3[/color][COLOR=#6a5acd],[/color] [COLOR=#ff00ff]9[/color][COLOR=#6a5acd],[/color] [COLOR=#ff00ff]8[/color])
}

[COLOR=#6a5acd]$2[/color] [COLOR=#6a5acd]~[/color] [COLOR=#ff00ff]/physical-memory/[/color] {
  physicalmemory_total [COLOR=#6a5acd]=[/color] [COLOR=#6a5acd]$5[/color]
  physicalmemory_proc [COLOR=#6a5acd]=[/color] [COLOR=#6a5acd]$6[/color]
}

[COLOR=#6a5acd]$2[/color] [COLOR=#6a5acd]~[/color] [COLOR=#ff00ff]/virtual-memory/[/color] {
  virtualmemory_total [COLOR=#6a5acd]=[/color] [COLOR=#6a5acd]$5[/color]
  virtualmemory_proc [COLOR=#6a5acd]=[/color] [COLOR=#6a5acd]$6[/color]
}

[COLOR=#6a5acd]$2[/color] [COLOR=#6a5acd]~[/color] [COLOR=#ff00ff]/footer/[/color] {
  enddate [COLOR=#6a5acd]=[/color] [COLOR=#008b8b]substr[/color]([COLOR=#6a5acd]$3[/color][COLOR=#6a5acd],[/color] [COLOR=#ff00ff]1[/color][COLOR=#6a5acd],[/color] [COLOR=#ff00ff]8[/color])
  endtime [COLOR=#6a5acd]=[/color] [COLOR=#008b8b]substr[/color]([COLOR=#6a5acd]$3[/color][COLOR=#6a5acd],[/color] [COLOR=#ff00ff]9[/color][COLOR=#6a5acd],[/color] [COLOR=#ff00ff]8[/color])
}

[COLOR=#6a5acd]END[/color] {
  [COLOR=#a52a2a][b]printf[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]%s[/color][COLOR=#ff00ff],[/color][COLOR=#6a5acd]%s[/color][COLOR=#ff00ff],[/color][COLOR=#6a5acd]%s[/color][COLOR=#ff00ff],[/color][COLOR=#6a5acd]%s[/color][COLOR=#ff00ff],[/color][COLOR=#6a5acd]%s[/color][COLOR=#ff00ff],[/color][COLOR=#6a5acd]%s[/color][COLOR=#ff00ff],[/color][COLOR=#6a5acd]%s[/color][COLOR=#ff00ff],[/color][COLOR=#6a5acd]%s[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color][COLOR=#6a5acd],[/color]
    [COLOR=#ff00ff]"startdate"[/color][COLOR=#6a5acd],[/color] [COLOR=#ff00ff]"starttime"[/color][COLOR=#6a5acd],[/color] [COLOR=#ff00ff]"eddate"[/color][COLOR=#6a5acd],[/color] [COLOR=#ff00ff]"endtime"[/color][COLOR=#6a5acd],[/color] 
    [COLOR=#ff00ff]"%physicalmemory"[/color][COLOR=#6a5acd],[/color] [COLOR=#ff00ff]"physicalmemory"[/color][COLOR=#6a5acd],[/color] [COLOR=#ff00ff]"%virtualmemory"[/color][COLOR=#6a5acd],[/color] [COLOR=#ff00ff]"virtualmemory"[/color]
  [COLOR=#a52a2a][b]printf[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]%s[/color][COLOR=#ff00ff],[/color][COLOR=#6a5acd]%s[/color][COLOR=#ff00ff],[/color][COLOR=#6a5acd]%s[/color][COLOR=#ff00ff],[/color][COLOR=#6a5acd]%s[/color][COLOR=#ff00ff],[/color][COLOR=#6a5acd]%s[/color][COLOR=#ff00ff],[/color][COLOR=#6a5acd]%s[/color][COLOR=#ff00ff],[/color][COLOR=#6a5acd]%s[/color][COLOR=#ff00ff],[/color][COLOR=#6a5acd]%s[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color][COLOR=#6a5acd],[/color]
    startdate[COLOR=#6a5acd],[/color] starttime[COLOR=#6a5acd],[/color] enddate[COLOR=#6a5acd],[/color] endtime[COLOR=#6a5acd],[/color] 
    physicalmemory_proc[COLOR=#6a5acd],[/color] physicalmemory_total[COLOR=#6a5acd],[/color] virtualmemory_proc[COLOR=#6a5acd],[/color] 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.
 
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
 
dlicheri said:
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
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top