Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

rows insteads of columns

Status
Not open for further replies.

hyperphoenix

Technical User
Sep 4, 2008
20
US
Is there a way to print found data in rows instead of a vertical column?

overview:

using this to find data in a txt file



while ( <FILE> )
{
if( /1stfield/ || /2ndfield/)
#I'm searching for strings with the word '1stfield' and '2ndfield.'
{


and using print to poll data - need the data in rows not columns on print. is this possible?
 
possibly something like this ---
This doesn't work but i think you'll be able to see what I'm look for with it...

Trying to get data that is polled like this

1234
4567
abcd
Same

To poll like this instead

1234 abcd
4567 same


************************ something like this ***********

#!/usr/bin/perl -w

use strict;
use warnings;

open(Source,"test.txt") || die "Cannot open file: $!";
open Export, ">export.xls";
select Export;


my @rows; # Contain each "record"
my $row = -1; # Will be incremented to 0 shortly


LINE:
while (<>) {
chomp;
# Get the field name and rest of the line
my ( $field_name, $field_val ) = unpack 'A20A*', $_;

# Skip blank lines
next LINE if $field_name eq '';

# Strip trailing space from fixed length records
$field_name =~ s/ \s+ \z //xms;
$field_val =~ s/ \s+ \z //xms;

# Is it a new record
$row++ if $field_name eq 'date';

$rows[$row]->{$field_name} = $field_val;
}

for my $row_ref (@rows) {
for my $field qw( Name Emplid Rcd rmed Department Job Code Termination Date ) {
print $row_ref->{$field}, "\t";
}
print $_;
}

 
maybe i should have mentioned I'm using active perl in windows
 
I'm not sure if I understand. You keep saying 'poll'.

Please provide an example of the input data, and an example of how you want it to look on the output.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
sorry used the wrong terminology. I'll show you what I have
and what I'm looking to accomplish.

pulling data from a txt document similar to this:

**************************************************

From ???@??? Wed, Jan Sep 2008 10:52:56 -0400
Date: Wed, 10 Jan 2008 10:52:56 -0400
From: unknown@mail.com
To: unknown@mail.com
Subject: term data

This employee has terminated and is transferring

Name: john smith
Emplid: 124700
Rcd#: 5
Termed from Company: Unknown
Department ID: Z53114200 - Tech Center
Job Code: 333181 - The Hamburglar
Termination Date: 2008-10-29
Transferred: to another company.


**********************************************************


Required text being pulled vertically. my only real problem is the data don't contain the same number of fields in each block or i could use a macro in excel to perform this function which has worked in the past. The data is currently being pulled vertically like this. When I open it in excel it's in a column. I need it in a row.

Name: john smith
Emplid: 124700
Rcd#: 5
Termed from Company: Unknown
Department ID: Z53114200 - Tech Center
Job Code: 333181 - The Hamburglar
Termination Date: 2008-10-29
Transferred: to another company.

*******************************************************

This is more along the lines of what I'm looking for so that i can move to excel without trouble.

john smith 124700 5 unknown z53114200 - Tech Center
Susan White 348200 4 known a12349823 - Tech Room
 
you could do
for my $line(@file) {
my ($row, $data) = split /\:/, $line;
print "$data ";
}


print "\n";



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
I should add..
you probably want to
chomp @file;
after reading your file in and before you start processing the data. This gets rid of any extra \n's you read in.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
well I have killed this script - any suggestions appreciated.



#!/usr/bin/perl -w

use strict;
use warnings;


# location of read/open file:

open(Source,"test.txt") || die "Cannot open file: $!";
chomp @File;

# location of new/write file

open Export, ">export.xls";
select Export;


# unkown

my @rows; # Contain each "record"
my $row = -1; # Will be incremented to 0 shortly

# script that creates rows intead of columns

LINE:
while (<>) {
chomp;
# Get the field name and rest of the line
my ( $field_name, $field_val ) = unpack 'A20A*', $_;

# Skip blank lines
next LINE if $field_name eq '';

# Strip trailing space from fixed length records
$field_name =~ s/ \s+ \z //xms;
$field_val =~ s/ \s+ \z //xms;

# Is it a new record
$row++ if $field_name eq 'date';

$rows[$row]->{$field_name} = $field_val;
}


# desired search criteria

for my $line(@file) {
my $field qw( Name Emplid Rcd rmed Department Job Code Termination Date ) = split /\:/, $line;
print "$data ";
}


print "\n";
}
 

# location of new/write file

open Export, ">export.xls";
select Export;

looks suspicious, and you don't ever close Export or Source

You don't need this chomp @File; because you are not reading it to an array.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
still not able to get this script running with the data that I have --

is there a print command that works?

I have a working script but it displays in columns not rows .


 
script that i am currently using:

#!/usr/bin/perl -w

open(FILE,"source.txt") || die "Where is Source File?: $!";

open MYFILE, ">export.txt";
select MYFILE;

while ( <FILE> )
{
if (/searchfield1/ || /searchfield3/ || /searchfield4/ || /searchfield4/) #I'm searching for any of these strings.
{
print $_; # Now I'm printing lines with those names in a single column (need this to be rows)
}
}

close(FILE);
close (MYFILE);
 
Did you try travs69's suggestion? That works for me with your data -- of course you have to parse out your email headers first. The entire script could look like this:

Code:
#!/usr/bin/perl
use strict;

my $found_data = 0;
open(FILE, "foo.txt");
while(<FILE>) {
    chomp;
    next if $found_data == 0 && $_ !~ /^Name:/;
    $found_data++;

    my ($row, $data) = split /\:/, $_;
    print "$data ";
}

print "\n";

--
 
This is what foo.txt looks like:

Code:
From ???@??? Wed, Jan Sep 2008 10:52:56 -0400
Date: Wed, 10 Jan 2008 10:52:56 -0400
From: unknown@mail.com
To: unknown@mail.com
Subject: term data

This employee has terminated and is transferring

Name:  john smith
Emplid:  124700
Rcd#:  5
Termed from Company:  Unknown
Department ID:  Z53114200  -  Tech Center
Job Code:  333181  -  The Hamburglar
Termination Date:  2008-10-29
Transferred: to another company.

--
 
thanks -- works perfectly.

do you work for tek-tips or just do this for fun?
 
We all work for tek-tips.. they pay really good :)


:p

j/k

We're probably all just bored nerds!!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top