Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
|
05-07-2012, 03:25 PM
|
#1
|
LQ Newbie
Registered: Apr 2012
Posts: 16
Rep: 
|
Help with Awk and Printf, please!
Hi guys. I need help with getting a file to give me a certain output using the awk command, please.
I have this file that has the following format:
SalesID:LastName:FirstName:MI
I need an output that is going to be like this:
LastName, FirstName MI SalesID
NOTE: all 3 names have to fit into a 25 character container and the SalesID needs to be right alligned. SalesID is a number.
So far, here is what I have.
I have a file called "salespeople" with data in a format I described above.
the command I have so far is:
awk -F: '{printf "%s, %-s %-s %d\n", $2, $3, $4, $1}' salespeople
Please tell me what I'm doing wrong. This is kind of urgent too.
Thanks in advance!
|
|
|
05-07-2012, 04:31 PM
|
#2
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
First of all, w/o knowing the lengths of all parts of the line this
is going to be impossible, full stop.
Code:
1001:Duckstoning:Donaldiorrrr:D
That's 26 characters there and then, w/o padding. No way to fit this
into 25, is there?
That said:
Code:
cat salesperson
1001:Duckston:Donaldiorrrr:D
awk -F: '{printf "%-25s %08d\n",$2", "$3" "$4, $1}' salesperson
Duckston, Donaldiorrrr D 00001001
Last edited by Tinkster; 05-07-2012 at 04:38 PM.
Reason: That said: ;)
|
|
|
05-07-2012, 04:59 PM
|
#3
|
LQ Newbie
Registered: Apr 2012
Posts: 16
Original Poster
Rep: 
|
I actually figured this out on my own. But, with a hint from the class instructor. The solution is to pipe an awk into another awk.
awk -F: '{printf "%s, %s %s: %s\n", $2, $3, $4, $1}' salespeople | awk -F: '{printf "%-25.25s %s\n", $1, $2}'
Thanks anyway!
Last edited by romeo0307; 05-07-2012 at 05:11 PM.
|
|
|
05-07-2012, 05:54 PM
|
#4
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Quote:
Originally Posted by romeo0307
I actually figured this out on my own. But, with a hint from the class instructor. The solution is to pipe an awk into another awk.
awk -F: '{printf "%s, %s %s: %s\n", $2, $3, $4, $1}' salespeople | awk -F: '{printf "%-25.25s %s\n", $1, $2}'
Thanks anyway!
|
Tell your instructor that's poor resource management, as I've shown above that
it can be done in one statement :P
Last edited by Tinkster; 05-07-2012 at 07:10 PM.
|
|
|
05-07-2012, 06:26 PM
|
#5
|
LQ Newbie
Registered: Apr 2012
Posts: 16
Original Poster
Rep: 
|
Quote:
Originally Posted by Tinkster
Tell your instructor that's poor resource management, as I've show above that
it can be done in one statement :P
|
Hm, you're right  Though, to make it same I had to take out comma after %-25s. Thanks though! Thanks for taking your time and responding.
|
|
|
05-07-2012, 06:29 PM
|
#6
|
LQ Newbie
Registered: Apr 2012
Posts: 16
Original Poster
Rep: 
|
By the way, sir, could you explain your code? I'm not getting how you constructed it. The quotes got me all confused.
|
|
|
05-07-2012, 07:14 PM
|
#7
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
All I did was to create a format-string w/ two parameters, one for
string data, one for numeric data, and assembled the "string" out of
the name chunks on the fly....
Btw, I didn't have a comma after %-25s ;}
Code:
{printf "%-25s %08d\n",$2", "$3" "$4, $1}
The second red chunk is ONE string, not three, using awk concatenation.
In detail:
So everything that isn't red is just part of the string which glues
the awk fields together.
Cheers,
Tink
Last edited by Tinkster; 05-07-2012 at 07:15 PM.
|
|
|
05-07-2012, 07:15 PM
|
#8
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,442
|
I like post #4, made me smile this morning 
|
|
|
05-07-2012, 07:22 PM
|
#9
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Quote:
Originally Posted by chrism01
I like post #4, made me smile this morning 
|
/me takes a bow: "Why thank you, kind Sir!" =o)
Always pleased to have some entertainment value ;D
Cheers,
Tink
|
|
|
05-07-2012, 07:33 PM
|
#10
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,442
|
I just want to see the instructor's face if the OP has the courage to tell/show him 
|
|
|
05-07-2012, 07:48 PM
|
#11
|
LQ Newbie
Registered: Apr 2012
Posts: 16
Original Poster
Rep: 
|
Quote:
Originally Posted by chrism01
I just want to see the instructor's face if the OP has the courage to tell/show him 
|
no way 
|
|
|
05-07-2012, 07:50 PM
|
#12
|
LQ Newbie
Registered: Apr 2012
Posts: 16
Original Poster
Rep: 
|
Quote:
Originally Posted by Tinkster
All I did was to create a format-string w/ two parameters, one for
string data, one for numeric data, and assembled the "string" out of
the name chunks on the fly....
Btw, I didn't have a comma after %-25s ;}
Code:
{printf "%-25s %08d\n",$2", "$3" "$4, $1}
The second red chunk is ONE string, not three, using awk concatenation.
In detail:
So everything that isn't red is just part of the string which glues
the awk fields together.
Cheers,
Tink
|
that's deep
a bit too deep for me lol
|
|
|
05-07-2012, 08:05 PM
|
#14
|
LQ Newbie
Registered: Apr 2012
Posts: 16
Original Poster
Rep: 
|
Quote:
Originally Posted by chrism01
|
the guy took away points from last assignment for excluding question numbers from the problems on the paper that i submitted. it had all the questions and answers and all were 100% correct. way too picky.
|
|
|
05-07-2012, 08:11 PM
|
#15
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,442
|
Sigh ... 
A good course should encourage learning the tool, not just canned qns/answers, otherwise you can only copy/paste, not actually deal with the real world .
Never mind, just keep the good stuff in your back pocket for afterwards 
|
|
|
All times are GMT -5. The time now is 06:38 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|