Operating System - Linux
1748117 Members
3640 Online
108758 Solutions
New Discussion юеВ

Re: printf formatting help

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

printf formatting help

Hello all,

I need some help with printf ....

I have a file that looks like:

SAL:FPGW:Running
SAL:runmqlsr:Running
SKM:FPGW:Running
SKM:runmqlsr:Running

I want to print the fields accross the screen to a certain format:

FPGW Running Running
runmqlsr Running Running

so I write an awk statement:

awk -F":" '/^SAL/ {(SAL = $3);/^SKM/ (SKM = $3); printf "%-15s\t%10s\n", $2,SAL"\t"SKM}'

and the output looks like:

FPGW Running Running
runmqlsr Running Running

however I want it to look like this and cant work it out - please help :-)

FPGW Running Running
runmqlsr Running Running

thanks

Chris.
hello
11 REPLIES 11
lawrenzo_1
Super Advisor

Re: printf formatting help

hmmmm its not printed to the web as I expected so I will use "\t" to denote the tabs:

FPGW"\t\t"Running"\t\t\t\t\t\t"Running
runmqlsr"\t\t"Running"\t\t\t\t\t\t"Running

thanks
hello
lawrenzo_1
Super Advisor

Re: printf formatting help

I have a solution but pretty sure this can be done better with proper printf formatting:

awk -F":" '/^SAL/ {(SAL = $3);/^SKM/ (SKM = $3); printf " %-15s\t%0s\n", $2,SAL"\t\t\t\t\t\t"SKM}' FILE.
hello
Dennis Handly
Acclaimed Contributor

Re: printf formatting help

I'm not sure what you are trying to do. Nor even why awk doesn't complaint about your syntax. You seem to have:
awk -F":" '
/^SAL/ {
(SAL = $3) # why () here?
/^SKM/ (SKM = $3) # HUH??
printf "%-15s\t%10s\n", $2,SAL"\t"SKM
} '

I have no idea what "^SKM/ (SKM = $3)" does. It seems to always do the assignment.

If you want two tabs between the first %s and the next, you need to replace that %-15s\t%10s by just %s\t\t%s.

Also, why bother with SAL"\t"SKM, when you can do that in the printf format? (printf may be slower than concatenation though.)
James R. Ferguson
Acclaimed Contributor

Re: printf formatting help

Hi Chris:

Another way -- a hash (associative array) to hold the data and print it afterwards:

# perl -ne 'chomp;@a=split /:/;push(@{$line{$a[1]}},$a[2]);END{for $x (sort keys %line) {printf "%-10s %s\n",$x,"@{$line{$x}"}}}' file

...yields:

FPGW Running Running
runmqlsr Running Running

Regards!

...JRF...
lawrenzo_1
Super Advisor

Re: printf formatting help

Thanks both,

Dennis I am still getting to grips with awk so I am not sure of any other way.

I try putting parenthesis around section of the awk but do not get the required output.

what do you suggest?
hello
Dennis Handly
Acclaimed Contributor

Re: printf formatting help

You haven't specified what you want, just the column format. Start with this:
SAL:FPGWL:Running1
SAL:runmqlsrL:Running2
SKM:FPGWM:Running3
SKM:runmqlsrM:Running4

What do you want for the output?
lawrenzo_1
Super Advisor

Re: printf formatting help

I think I got it:

awk -F":" '
/^SAL/ {
SAL = $3
/^SKM/
SKM = $3
print $2,SKM,SAL}' FILE

(without the printf off course .....
hello
lawrenzo_1
Super Advisor

Re: printf formatting help

FPGW Running Running
runmqlsr Running Running

with tabs formatting to the screen - which I now have from the printf.

This is being added to a script where SAL and SKM are 2 sites and the next field are the services running or not ....

thanks all the same

Chris.
hello
Dennis Handly
Acclaimed Contributor
Solution

Re: printf formatting help

>I think I got it:
awk -F":" '
/^SAL/ {
SAL = $3
/^SKM/
SKM = $3
print $2,SKM,SAL}' FILE

I don't see how.
The first RE makes sure it starts with SAL. The second RE is nested within the first condition so it can't be true at the same time.

If you are always ordered SAL then SKM, perhaps you meant:
/^SAL/ {SAL = $3; next}
/^SKM/ {
SKM = $3
print $2,SKM,SAL}' FILE