LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help with Awk and Printf, please! (https://www.linuxquestions.org/questions/linux-newbie-8/help-with-awk-and-printf-please-943747/)

romeo0307 05-07-2012 03:25 PM

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!

Tinkster 05-07-2012 04:31 PM

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


romeo0307 05-07-2012 04:59 PM

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!

Tinkster 05-07-2012 05:54 PM

Quote:

Originally Posted by romeo0307 (Post 4672784)
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

romeo0307 05-07-2012 06:26 PM

Quote:

Originally Posted by Tinkster (Post 4672809)
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.

romeo0307 05-07-2012 06:29 PM

By the way, sir, could you explain your code? I'm not getting how you constructed it. The quotes got me all confused.

Tinkster 05-07-2012 07:14 PM

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:
Code:

$2", "$3" "$4
So everything that isn't red is just part of the string which glues
the awk fields together.


Cheers,
Tink

chrism01 05-07-2012 07:15 PM

I like post #4, made me smile this morning :)

Tinkster 05-07-2012 07:22 PM

Quote:

Originally Posted by chrism01 (Post 4672855)
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

chrism01 05-07-2012 07:33 PM

I just want to see the instructor's face if the OP has the courage to tell/show him ;)

romeo0307 05-07-2012 07:48 PM

Quote:

Originally Posted by chrism01 (Post 4672865)
I just want to see the instructor's face if the OP has the courage to tell/show him ;)

no way :)

romeo0307 05-07-2012 07:50 PM

Quote:

Originally Posted by Tinkster (Post 4672853)
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:
Code:

$2", "$3" "$4
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

chrism01 05-07-2012 07:57 PM

Just submit the efficient answer and see what happens ;)

Here's a good page on printf; as it says, its basically the same as implemented in C (in fact its probably just a front-end for the same lib call :) ) http://linuxconfig.org/bash-printf-s...-with-examples

Good awk guide http://www.grymoire.com/Unix/Awk.html

romeo0307 05-07-2012 08:05 PM

Quote:

Originally Posted by chrism01 (Post 4672883)
Just submit the efficient answer and see what happens ;)

Here's a good page on printf; as it says, its basically the same as implemented in C (in fact its probably just a front-end for the same lib call :) ) http://linuxconfig.org/bash-printf-s...-with-examples

Good awk guide http://www.grymoire.com/Unix/Awk.html

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.

chrism01 05-07-2012 08:11 PM

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 09:59 AM.