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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
09-02-2008, 04:57 AM
|
#1
|
|
Member
Registered: Jul 2006
Location: Balen, Belgium
Distribution: Suse 10, Centos, Open Solaris
Posts: 76
Rep:
|
addings columns in perl script
Hi,
First of all..i'm a beginner in perl scripting.
I have an output-file like this:
Mike Bax tel-41596 start Fri 8/22 13:31
Thomas van Damme tel-92248 start Tue 9/2 8:56
I would like to change this in something like this:
Name: Telephone: Start Time:
Mike Bax tel-41596 Fri 8/22 13:31
Thomas van Damme tel-92248 Tue 9/2 8:56
How can I create columns within Perl?
Note: After submitting this post I see that this forum doesn't include my spaces. I need a columns with Name:, a columns with telephone:, and a seperate columns Start Time.
Last edited by activeq; 09-02-2008 at 04:59 AM.
|
|
|
|
09-02-2008, 09:47 AM
|
#2
|
|
Member
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 714
Rep:
|
Hi.
If you use CODE tags your intent may be more visible to us.
Select the text in the editing window, then click the # just above the window. That will surround the text with the appropriate tags
cheers, makyo
|
|
|
|
09-02-2008, 12:08 PM
|
#3
|
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,321
|
To attempt to answer your actual question, I suggest looking at the printf()/sprintf() functions, which are good at creating formatted output. In your case, I assume you are interested in padding and justifying text in fixed-width columns, so focus on the use of field-width specifiers.
Code:
printf( "%30s %-30s start %25s", $name, $phone, $startTime );
--- rod.
|
|
|
|
09-02-2008, 04:26 PM
|
#4
|
|
Senior Member
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282
Rep:
|
printf is enough for this case, but perl format should be mentioned imho, else why use perl
Code:
my($name, $telephone, $stime);
format Infos =
@<<<<<<<<<<<<<<<<<<@<<<<<<<<<<<<<<<<<<<@<<<<<<<<<<<<<<<
$name, $telephone, $stime
.
$~ = 'Infos';
($name, $telephone, $stime) = ('Name:', 'Telephone:', 'Start Time:');
write;
($name, $telephone, $stime) = ('Mike Bax', 'tel-41596', 'Fri 8/22 13:31');
write;
Last edited by keefaz; 09-02-2008 at 04:28 PM.
|
|
|
|
09-02-2008, 05:28 PM
|
#5
|
|
Member
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 714
Rep:
|
Hi.
If your goal is to learn perl, then by all means apply the suggestions provided above and continue to practice writing perl.
If your goal to obtain column alignment, then you may be interested in the results from S Kinzler's perl script:
Code:
#!/bin/bash -
# @(#) s1 Demonstrate Kinzler's perl align script.
# See: http://www.cs.indiana.edu/~kinzler/align/
echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1)
set -o nounset
echo
FILE=${1-data1}
echo " Data file $FILE:"
cat -A $FILE
echo
echo " Results, align default:"
align $FILE
echo
echo " Results, align with options:"
align -s t -j _ -a dddN $FILE
exit 0
Producing with your data, augmented by TAB delimiters and an extra numeric column:
Code:
% ./s1
(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0
Data file data1:
Name:^ITelephone:^IStart Time:^IA number$
Mike Bax^Itel-41596^IFri 8/22 13:31^I43.5$
Thomas van Damme^Itel-92248^ITue 9/2 8:56^I726.129$
Results, align default:
Name: Telephone: Start Time: A number
Mike Bax tel-41596 Fri 8/22 13:31 43.5
Thomas van Damme tel-92248 Tue 9/2 8:56 726.129
Results, align with options:
Name: Telephone: Start Time: A number
Mike Bax tel-41596 Fri 8/22 13:31 43.500
Thomas van Damme tel-92248 Tue 9/2 8:56 726.129
Best wishes ... cheers, makyo
|
|
|
|
09-03-2008, 01:17 AM
|
#6
|
|
Member
Registered: Jul 2006
Location: Balen, Belgium
Distribution: Suse 10, Centos, Open Solaris
Posts: 76
Original Poster
Rep:
|
Thanks for your replies.
I think I know how to do this for my setup, and the solution of Keefaz is the easiest one i think. Thanks for that.
Regards.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 02:31 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|