formating array file output using perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting formating array file output using perl
# 1  
Old 03-13-2002
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:

Code:
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

Last edited by Yogesh Sawant; 12-16-2009 at 01:47 AM.. Reason: added code tags
# 2  
Old 03-13-2002
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

Jimbo
# 3  
Old 03-23-2002
Bug

Hey thanks for that question and i got my answer also
# 4  
Old 03-22-2007
You can use xargs

Code:
seq 1 14 | xargs -n 6


Last edited by Yogesh Sawant; 12-16-2009 at 01:46 AM.. Reason: added code tags
# 5  
Old 03-22-2007
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);
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Storing the Linux command output to an array in perl script

Hi I am trying to store the output of a command into an array in perl script. I am able to store but the problem is i am unable to print the array line with one line space. i mean i inserted the \n in loop ...but not getting the result. I have written like this #!/usr/bin/perl @a =... (2 Replies)
Discussion started by: kumar85shiv
2 Replies

2. Shell Programming and Scripting

Formating output in html

Hi Guys, I was searching and landed up something here only. This is the code and I want the formatted html in email but that is not working, anybody knows the reason why? #!/bin/sh set -x DATE=`date -u` # Print beginning of webpage function html_header { cat <<END ... (5 Replies)
Discussion started by: bluemind2005
5 Replies

3. UNIX for Advanced & Expert Users

remove print formating from printer output file

I have a print file taken from the print spooler and I want to delete all the formatting leaving only the text. If you vi the file it shows "\304\304 ...." which translates into a printed line on print output. I need to be able to edit and pass this file to another process Thnaks (10 Replies)
Discussion started by: petercp
10 Replies

4. Shell Programming and Scripting

Array in Perl - Detect several file to be in one array

Hi everyone I have one question about using array in perl. let say I have several log file in one folder.. example test1.log test2.log test3.log and the list goes on.. how to make an array for this file? It suppose to detect log file in the current directory and all the log file will... (3 Replies)
Discussion started by: sayachop
3 Replies

5. Shell Programming and Scripting

Formating output

Hello Team i have a file with following data (as columns). I need implement a syntax like below for altering table ALTER TABLE1 TABLENAME ADD COLUMN COL1 CHAR(5) NOT NULL WITH DEFAULT ADD COLUMN COL2 CHAR(5) .. .. ADD COLUMN COLn CHAR(5) NOT NULL... (1 Reply)
Discussion started by: rocking77
1 Replies

6. Shell Programming and Scripting

formating output

Hi all, I want to start a new topic on this matter I have this script, #!perl use strict; use warnings; use Data::Dumper; open my $log, '>', 'log-external.txt' or die "Could not open log: $!"; print $log "Subnet,Static,DHCP,Unused\n"; open my $dump, '>', 'dump.log' or die... (2 Replies)
Discussion started by: richsark
2 Replies

7. Shell Programming and Scripting

file formating in Perl

Hi, I am new to unix , I have a requirement for formating the input file and generate the output file as per the downstream requirement .. My application receiving a text input file having 4 field and my application need to check each field and if some value of a field is blank ..then it need... (1 Reply)
Discussion started by: julirani
1 Replies

8. Shell Programming and Scripting

Output formating

Dear All I am stuck in one problem. Kindly help me. I am taking below mention file as input file and want some op file as mention below. Kindly send me all possible suggestion and query. Thnaks Jaydeep bELOW IS THE INPUT FILE: *** Connected to BSCANGR ***... (1 Reply)
Discussion started by: jaydeep_sadaria
1 Replies

9. Shell Programming and Scripting

formating output

I have a file proc.txt which contains the below one. Content-type: text/html <H2>No query</H2> infodba-marabou:/tmp => export QUERY_STRING="IMAN_server_report=full" infodba-marabou:/tmp => $IMAN_ROOT/web/htdocs/cgi-bin/iman > /tmp/proc.txt infodba-marabou:/tmp => cat proc.txt... (20 Replies)
Discussion started by: Krrishv
20 Replies

10. Shell Programming and Scripting

Formating cal output

Hi Gurus, In my Cal output i want to cut the date of 2nd saturday how tyo achive this. for eg in the below output i need that second saturday 13 to be cut. crypto $ cal January 2007 S M Tu W Th F S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26... (2 Replies)
Discussion started by: Krrishv
2 Replies
Login or Register to Ask a Question