Perl -- Script to re-format data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl -- Script to re-format data
# 1  
Old 05-27-2014
Perl -- Script to re-format data

Hi,

I have a file with data in the following format

Code:
BOX
-1.000000 -1.000000 0.000000
30.00000 14.00000 0.1000000
0.000000 0.000000 0.000000
0.000000
0.000000
CYLINDER
3.595000 2.995000 0.000000
0.5100000 2.000000
Z
0.000000 0.000000
0.000000

I want to convert these files into the following format instead
Code:
_Box -1.000000,-1.000000,0.000000 30.00000 14.00000 0.1000000
_Cylinder 3.595000,2.995000,0.000000 0.5100000 2.000000


Essentially these are the things I have done to re-format it.

1. Added "_" before BOX and CYLINDER and then changed these strings to lowercase (apart from the first letter).
2. The first line after BOX and CYLINDER moved to the same line as _Box and _Cylinder, but commas are added to separate the three numbers on that line.
3. The second line after BOX and CYLINDER moved to the same line as before - but separated using spaces.
4. Delete 3rd, 4th and 5th lines after BOX and CYLINDER.

I'd like to do this in perl if possible. So far I have the following script,

Code:
$infile="box_cyl.txt";
open(IN,$infile);
@lines=<IN>;

for ($i=1;$i<=$#lines;$i++){
	if($lines[$i] =~ 'CYLINDER'){
		@line1 = split (/ /,$lines[$i+2]);
                @line2 = split (/ /,$lines[$i+3]);
	} 
        elsif($lines[$i] =~ 'BOX'){
		@line1 = split (/ /,$lines[$i+2]);
                @line2 = split (/ /,$lines[$i+3]);
	}
}

Which basically finds the location of BOX and CYLINDER and reads the data in the first and second lines after BOX and CYLINDER is found. I'm new to perl and I'm not sure how to achieve the rest.

Many thanks!
# 2  
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:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
Login or Register to Ask a Question