Sponsored Content
Top Forums Shell Programming and Scripting Perl -- Script to re-format data Post 302903426 by Aia on Tuesday 27th of May 2014 03:16:07 PM
Old 05-27-2014
Perhaps a heavily commented and hard coded example?

Code:
#!/usr/bin/perl

use strict;
use warnings;

my $in="file";

open my $fh, '<', $in or die "Could not read $in: $!\n";

# start reading the file
while ( my $line = <$fh> ) {

    # find line with pattern BOX or CYLINDER
    if ( $line =~ /BOX|CYLINDER/ ) {

        chomp $line; # remove newline if exist

        my @captured_lines; # create a empty work space

        push @captured_lines, $line; # first store BOX or CYLINDER

        # we know the next two lines belongs with BOX or CYLINDER
        # let's get them
        foreach (1..2) {
            my $extra_line = <$fh>;
            chomp $extra_line;
            push @captured_lines, $extra_line;
        }

        # manipulate the strings
        $captured_lines[0] =~ s/(\w+)/_\u\L$1/g; # convert BOX into _Box and
                                                 # CYLINDER into _Cylinder
        $captured_lines[1] =~ s/ /,/g; # convert the spaces to `,' for
                                       # the first line after match

        print join (" ", @captured_lines) . "\n"; # display processed block
    }

    # optional: stop reading the file after manipulating CYLINDER
    if ( $line =~ /CYLINDER/ ) { last }
}
close $fh;


Last edited by Aia; 05-29-2014 at 12:07 AM.. Reason: code mispell
This User Gave Thanks to Aia For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to format a .CSV data

Hi There I needed to write a Unix shell script which will pick up the data from a .CSV file and reformat it as per the requirement and write it to another .CSV file. Currently I am in the proess of Data Import to "Remedy System" (A one kind of incident mangement Application) and this... (8 Replies)
Discussion started by: Uday1982
8 Replies

2. UNIX for Advanced & Expert Users

shell script to format .CSV data

Hi all, I have written a shell script to search a specified directory (e.g. /home/user) for a list of specific words (shown as ${TMPDIR}/wordlist below). The script works well enough, but I was wondering if there was a way to display the line number that the word is found on? Thanks! cat... (1 Reply)
Discussion started by: tmcmurtr
1 Replies

3. Shell Programming and Scripting

Need script to format data specifically

Hi all, I need a script specially using loops to print below output... variables x, a and b will be read from user... x a b x+1 a b+1 x+2 a b+2 x+3 a+1 b x+4 a+1 b+1 x+5 a+1 b+2 for example.... 1 ... (4 Replies)
Discussion started by: swapniltathe
4 Replies

4. Shell Programming and Scripting

Perl Script for reading table format data from file.

Hi, i need a perl script which reads the file, content is given below. and output in new file. TARGET DRIVE IO1 IO2 IO3 IO4 IO5 ------------ --------- --------- --------- --------- --------- 0a.1.8 266 236 ... (3 Replies)
Discussion started by: asak
3 Replies

5. Shell Programming and Scripting

Help with perl script to output data in table format...

Hello, I need help with a perl script that will process a text file and match virtual server name to profile(s). the rest will be ignored. Virtual server name follows the word "virtual" in the begging of the line. There could be multiple profiles assigned to one virtual server. For example, ... (3 Replies)
Discussion started by: besogon
3 Replies

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

7. UNIX for Dummies Questions & Answers

How to retrive data from DB(Aqua studio) in CVS format using UNIX script?

I am using aqua studio DB. I need to retrive the data from my database using uxin script in .csv format. i am using select query along with the joins. my o/p in the DB is of the below format. Cycle IDCycle StatusRecord 98N-0000ACV23-3636FCliet Level (Af)Success1689393HF-J7879-09090RCliet Level... (1 Reply)
Discussion started by: Mugivz
1 Replies

8. Shell Programming and Scripting

Script to generate Excel file or to SQL output data to Excel format/tabular format

Hi , i am generating some data by firing sql query with connecting to the database by my solaris box. The below one should be the header line of my excel ,here its coming in separate row. TO_CHAR(C. CURR_EMP_NO ---------- --------------- LST_NM... (6 Replies)
Discussion started by: dani1234
6 Replies

9. Shell Programming and Scripting

A script to format a file (ideally PERL)

Hi forum members. It has been several years since my last post. Currently I am using fairly large datasets on a day to day basis for handling immigration cases at a law firm. Our Input file is filled out by our secretary staff. The first column is the case ID-sample ID then the second column is... (9 Replies)
Discussion started by: kylle345
9 Replies

10. Shell Programming and Scripting

In PErl script: need to read the data one file and generate multiple files based on the data

We have the data looks like below in a log file. I want to generat files based on the string between two hash(#) symbol like below Source: #ext1#test1.tale2 drop #ext1#test11.tale21 drop #ext1#test123.tale21 drop #ext2#test1.tale21 drop #ext2#test12.tale21 drop #ext3#test11.tale21 drop... (5 Replies)
Discussion started by: Sanjeev G
5 Replies
vendorlib(3pm)						User Contributed Perl Documentation					    vendorlib(3pm)

NAME
vendorlib - Use Only Core and Vendor Libraries in @INC SYNOPSIS
#!/usr/bin/perl use vendorlib; use strict; use warnings; use SomeModule; # will only search in core and vendor paths ... DESCRIPTION
In a system distribution such as Debian, it may be advisable for Perl programs to ignore the user's CPAN-installed modules and only use the distribution-provided modules to avoid possible breakage with newer and unpackaged versions of modules. To that end, this pragma will replace your @INC with only the core and vendor @INC paths, ignoring site_perl and $ENV{PERL5LIB} entirely. It is recommended that you put "use vendorlib;" as the first statement in your program, before even "use strict;" and "use warnings;". BUGS
Please report any bugs or feature requests to "bug-vendorlib at rt.cpan.org", or through the web interface at <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=vendorlib>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. SUPPORT
You can find documentation for this module with the perldoc command. perldoc vendorlib You can also look for information at: o RT: CPAN's request tracker <http://rt.cpan.org/NoAuth/Bugs.html?Dist=vendorlib> o AnnoCPAN: Annotated CPAN documentation <http://annocpan.org/dist/vendorlib> o CPAN Ratings <http://cpanratings.perl.org/d/vendorlib> o Search CPAN <http://search.cpan.org/dist/vendorlib/> ACKNOWLEDGEMENTS
mxey and jawnsy on oftc #debian-perl helped to hash out the design for this. ribasushi reviewed the initial version and pointed out that @INC order matters. AUTHOR
Rafael Kitover, "<rkitover at cpan.org>" LICENSE AND COPYRIGHT
Copyright 2011 Rafael Kitover. This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License. See http://dev.perl.org/licenses/ for more information. perl v5.10.1 2011-01-17 vendorlib(3pm)
All times are GMT -4. The time now is 06:42 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy