LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 05-07-2012, 03:25 PM   #1
romeo0307
LQ Newbie
 
Registered: Apr 2012
Posts: 16

Rep: Reputation: Disabled
Unhappy 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!
 
Old 05-07-2012, 04:31 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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: ;)
 
Old 05-07-2012, 04:59 PM   #3
romeo0307
LQ Newbie
 
Registered: Apr 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
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.
 
Old 05-07-2012, 05:54 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by romeo0307 View Post
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.
 
Old 05-07-2012, 06:26 PM   #5
romeo0307
LQ Newbie
 
Registered: Apr 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Tinkster View Post
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.
 
Old 05-07-2012, 06:29 PM   #6
romeo0307
LQ Newbie
 
Registered: Apr 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
By the way, sir, could you explain your code? I'm not getting how you constructed it. The quotes got me all confused.
 
Old 05-07-2012, 07:14 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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

Last edited by Tinkster; 05-07-2012 at 07:15 PM.
 
Old 05-07-2012, 07:15 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,374

Rep: Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383
I like post #4, made me smile this morning
 
Old 05-07-2012, 07:22 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by chrism01 View Post
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
 
Old 05-07-2012, 07:33 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,374

Rep: Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383
I just want to see the instructor's face if the OP has the courage to tell/show him
 
Old 05-07-2012, 07:48 PM   #11
romeo0307
LQ Newbie
 
Registered: Apr 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by chrism01 View Post
I just want to see the instructor's face if the OP has the courage to tell/show him
no way
 
Old 05-07-2012, 07:50 PM   #12
romeo0307
LQ Newbie
 
Registered: Apr 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Tinkster View Post
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
 
Old 05-07-2012, 07:57 PM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,374

Rep: Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383
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
 
Old 05-07-2012, 08:05 PM   #14
romeo0307
LQ Newbie
 
Registered: Apr 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by chrism01 View Post
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.
 
Old 05-07-2012, 08:11 PM   #15
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,374

Rep: Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] problem with awk printf culin Linux - Newbie 15 11-28-2010 06:25 AM
awk printf ignoring '\n' MrUmunhum Linux - General 2 01-25-2010 02:00 PM
printf \b not working as expected in AWK Rockydell Programming 5 11-13-2009 07:28 AM
printf doesn't work in awk MheAd Programming 4 09-24-2008 01:46 PM
need help w/ awk & printf rickenbacherus Programming 3 05-13-2004 07:21 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration