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! |
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 into 25, is there? That said: Code:
cat salesperson |
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! |
Quote:
it can be done in one statement :P |
Quote:
|
By the way, sir, could you explain your code? I'm not getting how you constructed it. The quotes got me all confused.
|
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} In detail: Code:
$2", "$3" "$4 the awk fields together. Cheers, Tink |
I like post #4, made me smile this morning :)
|
Quote:
Always pleased to have some entertainment value ;D Cheers, Tink |
I just want to see the instructor's face if the OP has the courage to tell/show him ;)
|
Quote:
|
Quote:
a bit too deep for me lol |
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 |
Quote:
|
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. |