Text allignment using PERL


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Text allignment using PERL
# 1  
Old 06-30-2010
Question Text allignment using PERL

Hi Friends,

For daily reports i make perl script like below.

Code:
@dirlist = `cat out.txt |cut -d "|" -f1 >create.txt`; 

@dirlist1 = `cat out.txt|wc -l *e* >create2.txt`;

open FILE, ">OUTPUT.txt";
@command = `cat out.txt |cut -d "|" -f1`; print FILE map{$_-2 ."\n"}@command;

@dirlist2 = `paste create.txt create2.txt OUTPUT.txt > exeout.txt`;

@dirlist3= `cat exeout.txt`;

print "@dirlist3";

output is :

Code:
378462         4 allignment.pl   378460
 764jds         3 create.txt      762
 907834        0 create2.txt     907832
                  8 exeout.txt
                  21 test.pl
                  11 test2.pl
                  8 test3.pl
                   55 total

i expect the out put in correct allignment with table. we can give coloum name like col-1 col-2 col-3

Last edited by adaleru; 06-30-2010 at 04:26 AM.. Reason: code tags, please...
# 2  
Old 06-30-2010
First things first: Perl is not shell scripting!
Second: you're using cat far too often. cut can read a file fine by itself, and wc, when given a file argument, won't even bother reading stdin.
Third: see perldoc perlform for Perl formatting instructions, which are ideal for report formatting.

---------- Post updated at 09:45 ---------- Previous update was at 09:29 ----------

(Almost) exactly your script, except without temporary files, pretty printing, and only 1 external call:
Code:
#!/usr/bin/perl

use strict;
use warnings;

my ( @dirlist, @dirlist1, @command );
my ( $dir,     $dir1,     $cmd );

format STDOUT_TOP=
+----------+---------------------+---------+
| Col 1    | Col 2               | Col 3   |
+----------+---------------------+---------+
.

format STDOUT=
| @<<<<<<< | @|||||||||||||||||| | @>>>>>>>|
  $dir,      $dir1,                $cmd
.

format STDOUT_BOTTOM=
+----------+---------------------+---------+
.

open my $fh, '<', 'out.txt';
@dirlist = map { @_ = split /\|/; $_[0] } <$fh>;
close $fh;
@command = map { $_ - 2 } @dirlist;
@dirlist1 = split /\n/, qx/wc -l *e*/;

for ( my $i = 0 ; $i < scalar @dirlist1 ; $i++ ) {
    $dir  = $dirlist[$i];
    $dir  = '' unless defined $dir;
    $dir1 = $dirlist1[$i];
    $dir1 = '' unless defined $dir1;
    $cmd  = $command[$i];
    $cmd  = '' unless defined $cmd;
    write;
}
$~ = 'STDOUT_BOTTOM';
write;

Will result in:
Code:
+----------+---------------------+---------+
| Col 1    | Col 2               | Col 3   |
+----------+---------------------+---------+
| 378462   |      3 test1.txt    |   378460|
| 764      |      6 test2.txt    |      762|
| 907834   |      1 test3.txt    |   907832|
|          |       10 total      |         |
+----------+---------------------+---------+

And you can adjust the formatting as you like, without having to change the whole code.
This User Gave Thanks to pludi For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Allignment input file

Guys, I have a requirement as below 36%/ 55%/var 82%/tmp 74%/opt now i want the output to be --------------------------------- Disk% Mount --------------------------------- 36% / 55% var 82% tmp 74% opt ---------------------------------------- I have used... (3 Replies)
Discussion started by: AraR87
3 Replies

2. Shell Programming and Scripting

awk - script help: column to row format of data allignment?

Experts Good day, I have the following data, file1 BRAAGRP1 A2X B2X C2X D2X BRBGRP12 A3X B3X Z10 D09 BRC1GRP2 LO01 (4 Replies)
Discussion started by: rveri
4 Replies

3. Shell Programming and Scripting

Output allignment

Hi Guys, I hope you are doing good out there. I am facing some issues in the alligment of the output of a shell script. Below is the statement which is formatting the output:echo $File | awk -F '' '{print $13,$15="\t"$16,$4="",$5,$6,$7}' and its output is Domain Log file ... (2 Replies)
Discussion started by: singh.chandan18
2 Replies

4. Shell Programming and Scripting

linux 'paste' allignment problem

Hi Everyone, # cat 1 #!/usr/bin/perl print "c\tc\t\n"; # cat 2 #!/usr/bin/perl print "b\tb\t\n"; print "b\tb\t\n"; print "b\tb\t\n"; # perl 1 > 11 # perl 2 > 22 # cat 11 c c # cat 22 b b b b b b # paste 11 22 (5 Replies)
Discussion started by: jimmy_y
5 Replies

5. Shell Programming and Scripting

Perl Text manipulation

Hello All, I have been working on a great script to remotely gather server info and store it in a .txt that can be imported to .xls I have been reading the hostnames that are in the /.shh/known_hosts file so I don't have to mess with passing a password - via ssh (not easy to do , by the... (1 Reply)
Discussion started by: dfezz1
1 Replies

6. Shell Programming and Scripting

Allignment

Hi All, I want to shift the last and second last line to 8 spaces in the right inside a file. please can somebody suggest a script for the same. Thanks and Best Regards, Shazin (22 Replies)
Discussion started by: Shazin
22 Replies

7. Shell Programming and Scripting

Allignment of lines in a file

Hi All, I am using the below scrit to insert lines in a file: #!/bin/ksh # To delete the last line if it contains the pattern Redirect permanent / Virgin Atlantic Airways - Popup echo "Enter the URL that should point to the particular microsite" read url # To delete the last line if it... (2 Replies)
Discussion started by: Shazin
2 Replies

8. Shell Programming and Scripting

Perl - Inserting text

Hey, I have 10 lines of text ... And I would like to Insert prefix for each line with static text. perl -pi -e 's/()/$1 TEST$./' data.txt Each line will have different static prefix, Code above works perfectly for 1st line ... I'm just not sure how I can run same command for 2nd line 3rd... (4 Replies)
Discussion started by: NDxiak
4 Replies

9. Shell Programming and Scripting

PERL Text Munging

I have a file like this: host1,neighbor1 host3,neighbor4 host2,neighbor1 host2,neighbor3 host1,neighbor3 host3,neighbor1 host1,neighbor2 host3,neighbor2 I need some PERL magic that will generate output like this: host1,neighbor1,neighbor3,neighbor2 host2,neighbor1,neighbor3... (3 Replies)
Discussion started by: stateful
3 Replies

10. UNIX for Advanced & Expert Users

Need help take out few text in perl

I have perl program and I know that while sending an e-mail the following code returns "Remote (/opt/seasoft/db/nervecenter.nms00tst1):" in an email, I need to take out the entire "opt/seasoft/db/nervecenter" and leave with "ms00tst1" only. How do I do it: So the output would be "Remote... (3 Replies)
Discussion started by: amir07
3 Replies
Login or Register to Ask a Question

Featured Tech Videos