The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
perl -write values in a file to @array in perl meghana Shell Programming and Scripting 27 06-07-2009 05:05 PM
Problem In Formating Table as Output Lo11001 Shell Programming and Scripting 1 05-12-2008 06:35 AM
Output formating jaydeep_sadaria Shell Programming and Scripting 1 04-10-2008 12:39 PM
formating output Krrishv Shell Programming and Scripting 20 02-02-2007 09:39 AM
Formating cal output Krrishv Shell Programming and Scripting 2 01-11-2007 10:46 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-13-2002
seismic_willy seismic_willy is offline
Registered User
  
 

Join Date: Jan 2002
Location: Houston
Posts: 11
formating array file output using perl

Hello,

I am trying to output the values in an array to a file. The output needs to be formated such that each array value is left jusified in a field 8 character spaces long. Also, no more than 6 fields on a line. For example:

@array= 1..14;

Needs to be output to the file like so:

1 2 3 4 5 6
7 8 9 10 11 12
13 14

Of course, the amount of array values will vary.
Can't this be done with the printf function?

Please Help,
seismic_willy
  #2 (permalink)  
Old 03-13-2002
Jimbo
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
Well, here's a while command that uses printf to output a unix array, and it closes out the line after every 6th entry:

Code:
#!/bin/ksh

a[1]=this
a[2]=array
a[3]=is
a[4]=holding
a[5]=nine
a[6]=entries
a[7]=right
a[8]=now
a[9]=.

i=1
w=0
while [ "${a[$i]}" ]
do
if [ $w -eq 6 ] ; then
   printf "\n"
   w=0
fi
printf "%-8s" ${a[$i]}
((i=i+1))
((w=w+1))
done

printf "\n"
exit 0
  #3 (permalink)  
Old 03-23-2002
krkc4u krkc4u is offline
Registered User
  
 

Join Date: Mar 2002
Posts: 2
Smile

Hey thanks for that question and i got my answer also
  #4 (permalink)  
Old 03-21-2007
yogi_raj_143 yogi_raj_143 is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 16
You can use xargs

seq 1 14 | xargs -n 6
  #5 (permalink)  
Old 03-22-2007
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
Code:
#!/usr/bin/perl
my @num = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);
for my $i (0 .. $#num) {
    printf ("%-8d", $num[$i]);
    print "\n" if (($i + 1) % 6 == 0);
}
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate 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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:54 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0