- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: printf formatting help
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-28-2008 11:46 PM
тАО05-28-2008 11:46 PM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-28-2008 11:52 PM
тАО05-28-2008 11:52 PM
Re: printf formatting help
FPGW"\t\t"Running"\t\t\t\t\t\t"Running
runmqlsr"\t\t"Running"\t\t\t\t\t\t"Running
thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2008 12:23 AM
тАО05-29-2008 12:23 AM
Re: printf formatting help
awk -F":" '/^SAL/ {(SAL = $3);/^SKM/ (SKM = $3); printf " %-15s\t%0s\n", $2,SAL"\t\t\t\t\t\t"SKM}' FILE.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2008 02:34 AM
тАО05-29-2008 02:34 AM
Re: printf formatting help
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.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2008 04:01 AM
тАО05-29-2008 04:01 AM
Re: printf formatting help
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...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2008 04:45 AM
тАО05-29-2008 04:45 AM
Re: printf formatting help
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2008 04:49 AM
тАО05-29-2008 04:49 AM
Re: printf formatting help
SAL:FPGWL:Running1
SAL:runmqlsrL:Running2
SKM:FPGWM:Running3
SKM:runmqlsrM:Running4
What do you want for the output?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2008 04:49 AM
тАО05-29-2008 04:49 AM
Re: printf formatting help
awk -F":" '
/^SAL/ {
SAL = $3
/^SKM/
SKM = $3
print $2,SKM,SAL}' FILE
(without the printf off course .....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2008 04:52 AM
тАО05-29-2008 04:52 AM
Re: printf formatting help
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2008 05:00 AM
тАО05-29-2008 05:00 AM
Solutionawk -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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2008 05:15 AM
тАО05-29-2008 05:15 AM
Re: printf formatting help
I can see that now ....
I havent come across the "next" statement until now so much appreciated.
Chris.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2008 05:19 AM
тАО05-29-2008 05:19 AM
Re: printf formatting help
next wasn't really needed but it does simply the logic some. Just like return is better than deeply nested if blocks or flag programming. And of course better than gotos. :-)