LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 10-14-2011, 07:45 AM   #1
ed88
LQ Newbie
 
Registered: Apr 2008
Posts: 13

Rep: Reputation: 0
Unhappy printf problem


hi to all must say that im new in linux and programing, under Bash im try to make a script using a video tutorial (Unix Shell Scripting Advanced) and the problem come when i try to print a list of users from data file (names.dat)
format:
name : Asurname : address : city
namee : Csurname1 : address1 : city1
nameee : Bsurname2 : address2 : city2

the exercies is to make print of the names.dat sorted by the 2nd column (surname) printing whitout the ":" in format like this :

name Asurname address city
nameee Bsurname2 address2 city2
namee Csurname1 address1 city1

in the tutorial he use this comand:
Code:
sort -t : +1 names.dat | awk -F : '{printf("%-14.14s%-16.16s%-20.20s%-15.15s%-6.6s%-5.5s\n", $1, $2, $3, $4, $5, $6)}' | more
i have try it whitout to see the answer to make it alone but was imposible to me to make each column to be line iget somting like this:

name Asurname address city
nameee Bsurname2 address2 city2
namee Csurname1 address1 city1

i have tryed to copy the code from the tutorial but dont work

this is my code :
Code:
awk -F : '{"print(%-20.20s%-20.20s%-20.20s%-20.20s%-20.20s%-20.20s\n", $1, $2, $3, $4, $5, $6)}' names.dat | sort -k2 | more
maby the teacher in the tutorial use old version of the printf program whit my code i get somting like this :
Code:
%-20.20s%-20.20s%-20.20s%-20.20s%-20.20s%-20.20s
%-20.20s%-20.20s%-20.20s%-20.20s%-20.20s%-20.20s
%-20.20s%-20.20s%-20.20s%-20.20s%-20.20s%-20.20s
%-20.20s%-20.20s%-20.20s%-20.20s%-20.20s%-20.20s
%-20.20s%-20.20s%-20.20s%-20.20s%-20.20s%-20.20s
%-20.20s%-20.20s%-20.20s%-20.20s%-20.20s%-20.20s
MANY LINES OF THIS AND AFTER THE LIST OF NAMES 
name Asurname address city
nameee Bsurname2 address2 city2
namee Csurname1 address1 city1
and whit the tutorial code i dont get nothing

need help and explication who work printf and the columns in line

Last edited by ed88; 10-14-2011 at 07:48 AM.
 
Old 10-14-2011, 07:55 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

I see 2 typo's in your example:

Code:
'{"print(%-20.20s .....

# should be

'{printf("%-20.20s ....
All in all it should look like this:
Code:
 awk -F: '{ printf("%-20.20s%-20.20s%-20.20s%-20.20s%-20.20s%-20.20s\n", $1, $2, $3, $4, $5, $6) }' infile | sort -k2
Hope this helps.
 
1 members found this post helpful.
Old 10-14-2011, 08:04 AM   #3
ed88
LQ Newbie
 
Registered: Apr 2008
Posts: 13

Original Poster
Rep: Reputation: 0
so mamy thimes i have change it delete this like and the final i forgot the f after print i have another quiestion why the code from the tutorial dont work to me old version of printf ??
 
Old 10-14-2011, 08:08 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

The sort command (sort -t : +1) in your first example doesn't make sense, but the awk command works. I.e: nothing wrong with printf:
Code:
$ awk -F : '{printf("%-14.14s%-16.16s%-20.20s%-15.15s%-6.6s%-5.5s\n", $1, $2, $3, $4, $5, $6)}' infile 
name           Asurname        address             city                     
namee          Csurname1       address1            city1                    
nameee         Bsurname2       address2            city2
Hope this helps.
 
1 members found this post helpful.
Old 10-14-2011, 08:16 AM   #5
ed88
LQ Newbie
 
Registered: Apr 2008
Posts: 13

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by druuna View Post
Hi,

The sort command (sort -t : +1) in your first example doesn't make sense.
hmm maby there was the problem its posible old version of sort program ? or what he mean whit this (what do this) :

sort -t : +1
 
Old 10-14-2011, 08:26 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,
Quote:
Originally Posted by ed88 View Post
hmm maby there was the problem its posible old version of sort program ? or what he mean whit this (what do this) :

sort -t : +1
I'm not sure if it is an old version, maybe a version that belongs to SunOS/Solaris and/or HPUX.

Sorting a specific column, which I believe the +1 is for, is done with -k<col-number>, which should work with all sort version/flavors.

Just found this sort man-page: SunOS 5.10 - sort manual page. The +1 notation is mentioned here, so I guess your example should work on sunos/solaris.

Hope this helps.
 
1 members found this post helpful.
  


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
Using printf has a problem Pinglu Linux - Newbie 4 09-15-2011 02:53 PM
[SOLVED] A small problem on printf ?? alikamu Programming 6 07-25-2011 10:39 AM
[SOLVED] problem with awk printf culin Linux - Newbie 15 11-28-2010 06:25 AM
printf escaping problem Suinatsa Programming 3 10-25-2006 09:28 AM
printf problem??? Mistro116@yahoo.com Programming 2 11-13-2005 08:45 PM

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

All times are GMT -5. The time now is 10:35 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