LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-14-2010, 02:16 AM   #1
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Rep: Reputation: 15
Displaying information using awk


Hey guys, i am using awk to display my information in a certain order.

Database :
Code:
 Persia:42:John
France:50:Mabel 
Persia:50:Rach
Germany:60:John

My expected output is :

Code:
Title             Price           Author
Persia             42              John
France             50              Mabel
Persia             50              Rach
Germany            60              John
So i first tried using awk command which goes like this :
Code:
Title=$(awk -F":" '{printf"\n" $1 }' fruit)
       Price=$(awk -F":" '{printf"\n" $2 }' fruit)
       Author=$(awk -F":" '{printf"\n" $3 }' fruit)
Reason being, i was thinking by arranging the variables under the each Columm name such as Title, Price and Author, it will arrange itself neatly. Let me show u what i meant.

Code:
echo  Title             Price           Author
echo  $Title             $Price          $Author
but for some reason, when i save the results in a variable, the formatting will be gone, instead of displaying the information in a neat columm, it displays the information on one line like the example below :

Code:
Title             Price           Author
PersiaFrancePersiaGermany

I tried using cut command but it also gives me the same problem. If i do not assign it to a variable, how am i able to format it to display it in the correct format?

Last edited by gregarion; 01-14-2010 at 03:09 AM.
 
Old 01-14-2010, 02:24 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
well there's no newline character in the awk print, add a \n to the end of the print. Why bother assigning to a variable at all though? Just run the awk (or cut or other command) directly at the end.

And don't use `backticks`, use $(dollar bracket) notation instead for the executing stuff for output assignment.
 
Old 01-14-2010, 02:44 AM   #3
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Original Poster
Rep: Reputation: 15
Thanks alot. well, the reason i am assigning the it to a value is bacause i need to information to be displayed under the correct columm. FOr example, for Title , all its information has to be displayed under it. and for Author, all its information has to be displayed under it.

Could you explain to me, why is that when i save it to a value, the formatting runs away again? when i dont assign it to a value and use \n, it comes out fine, but once i assign it to a value it comes out in one straight line again.
Code:
My expected output was : 

Title             Author           Price 
Persia             John
France             Mary
Persia             Susan
Germany            Jhonny
this was why i assign it to a variable. so just underneath each Columm , i just have to put the $variable and display the information in this format.

Last edited by gregarion; 01-14-2010 at 03:00 AM.
 
Old 01-14-2010, 03:16 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Hmm, thanks for editing your original post to add in what I suggested... really really confusing and misleading, huh?
 
Old 01-14-2010, 03:19 AM   #5
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Original Poster
Rep: Reputation: 15
Sorry, i just wanted to highlight the main problem. if i do not assign it to a variable, how do i get it to display in the correct format? i tried just using the command without assigning it to a variable , but the issue i am getting is that it just displays the next batch of columm below the other one.
 
  


Reply



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] awk code help for displaying file contents ashok.g Programming 13 12-14-2009 12:55 AM
Displaying the OP title on the OP's information panel is a neat function. Dogs LQ Suggestions & Feedback 1 09-29-2009 11:58 AM
displaying bandwidth usage information on an network wanas Linux - Networking 15 04-17-2009 11:15 PM
LXer: Displaying System Information On Linux or Unix With Cfg2html LXer Syndicated Linux News 0 08-21-2008 09:41 AM
Displaying Process Information From task_struct smoothdogg00 Programming 1 12-19-2006 04:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:17 PM.

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
Open Source Consulting | Domain Registration