Sponsored Content
Full Discussion: Text allignment using PERL
Top Forums Shell Programming and Scripting Text allignment using PERL Post 302433569 by pludi on Wednesday 30th of June 2010 03:45:57 AM
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:
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
ppmtosixel(1)						      General Commands Manual						     ppmtosixel(1)

NAME
ppmtosixel - convert a portable pixmap into DEC sixel format SYNOPSIS
ppmtosixel [-raw] [-margin] [ppmfile] DESCRIPTION
Reads a portable pixmap as input. Produces sixel commands (SIX) as output. The output is formatted for color printing, e.g. for a DEC LJ250 color inkjet printer. If RGB values from the PPM file do not have maxval=100, the RGB values are rescaled. A printer control header and a color assignment table begin the SIX file. Image data is written in a compressed format by default. A printer control footer ends the image file. OPTIONS
-raw If specified, each pixel will be explicitly described in the image file. If -raw is not specified, output will default to com- pressed format in which identical adjacent pixels are replaced by "repeat pixel" commands. A raw file is often an order of magni- tude larger than a compressed file and prints much slower. -margin If -margin is not specified, the image will be start at the left margin (of the window, paper, or whatever). If -margin is speci- fied, a 1.5 inch left margin will offset the image. PRINTING
Generally, sixel files must reach the printer unfiltered. Use the lpr -x option or cat filename > /dev/tty0?. BUGS
Upon rescaling, truncation of the least significant bits of RGB values may result in poor color conversion. If the original PPM maxval was greater than 100, rescaling also reduces the image depth. While the actual RGB values from the ppm file are more or less retained, the color palette of the LJ250 may not match the colors on your screen. This seems to be a printer limitation. SEE ALSO
ppm(5) AUTHOR
Copyright (C) 1991 by Rick Vinci. 26 April 1991 ppmtosixel(1)
All times are GMT -4. The time now is 07:56 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy