10-10-2008
As I understand your problem, using Perl (and your sample data) I believe this will give you what you want.
#!/usr/bin/perl
my @fields;
my @hold;
my $key1;
my $x;
my $i;
open INPUT, "<file6";
open OUTPUT, ">outfile";
while(<INPUT>)
{
chomp;
@fields = split /,/, $_;
$key1 = $fields[0];
shift @fields;
while ( defined($fields[0]) )
{
for ($i = 1; $i < 7; $i++)
{
if ( defined($fields[0]) )
{
$hold[$i] = $fields[0];
shift @fields;
}
else
{
last;
}
}
$x = join ",", @hold;
$array1{$key1} = $x;
print OUTPUT "$key1$array1{$key1}\n";
@hold = ( );
}
}
close INPUT;
close OUTPUT;
10 More Discussions You Might Find Interesting
1. Shell Programming and Scripting
i have single column which is starting with same string(many number of rows)
i have to convert each into a single row.how can i do that?
laknar
std
mes
23
55
laknar
isd
phone no
address
amount
99
I have to convert above like below.
laknar|std|mes|23|55
laknar|isd|phone... (3 Replies)
Discussion started by: laknar
3 Replies
2. Shell Programming and Scripting
Hello Friends,
I am new to UNIX shell scripting. Using bash....Could you please help me in converting a flat file into an XML style output file.
Flat file: (Input File entries looks like this)
John Miller: 617-569-7996:15 Bunting lane, staten Island, NY: 10/21/79: 60600
The... (4 Replies)
Discussion started by: humkhn
4 Replies
3. UNIX for Advanced & Expert Users
Hi All,
I am new to this forum, could any one help me out in resolving the below issue.
Input of the flat file contains several lines of text for example find below:
5022090,2,4,7154,88,,,,,4/1/2011 0:00,Z,L,2
5022090,3,1,6648,88,,,,,4/1/2011 0:00,Z,,1... (0 Replies)
Discussion started by: av_sagar
0 Replies
4. UNIX for Dummies Questions & Answers
We receive a file which usually has 40 to 50 million rows. I want to set up an audit process by which everytime we receive a file we audit it for # of rows and total number of columns.
So if the # of rows is around 1 million on a particular day, I want to raise a flag or send an email....and if... (3 Replies)
Discussion started by: priya33184
3 Replies
5. Shell Programming and Scripting
Hi all,
I need your help on a multiple row entry into different columns. And do the same with all the entries in file.
File example (showing 2 entries only, there are many like these):
>ABC
*
AGA-AUUCUC-CGGUUCAAUCU
|||
UCUAUAACCGCGCCGAGUUAGU
>ABC
*
AGAUAU-GCUGCAGGCUCAAUUG
||||||... (2 Replies)
Discussion started by: atulkakrana
2 Replies
6. Shell Programming and Scripting
Hi Friends,
I want to convert a XML file to flat file.
Sample I/p:
<?xml version='1.0' encoding='UTF-8' ?>
<DataFile xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' contactCount='4999' date='2012-04-14' time='22:00:14' xsi:noNamespaceSchemaLocation='gen
.xsd'>
<Contact... (3 Replies)
Discussion started by: karumudi7
3 Replies
7. Shell Programming and Scripting
i have output of script as below
name,roll_no,01-05-12,02-05-12,03-05-12
sam,12,24,24,24
jon,145,24,24,22
van,29,24,22,24
i want to convert these into columns as output is not fixed please tell me how to convert 1st row in to 1st columns likewise,as many rows are there are to be converted... (4 Replies)
Discussion started by: sagar_1986
4 Replies
8. UNIX for Dummies Questions & Answers
Hi gurus!
Please help me with this one. I have an file with the following contents:
a
b
c
d
e
f
g
h
i
j
I would like to make to transform it to look like this as my output file:
a,b,c
d,e,f (4 Replies)
Discussion started by: kokoro
4 Replies
9. Shell Programming and Scripting
Dear fellows, I need your help.
I'm trying to write a script to convert a single column into multiple rows.
But it need to recognize the beginning of the string and set it to its specific Column number.
Each Line (loop) begins with digit (RANGE).
At this moment it's kind of working, but it... (6 Replies)
Discussion started by: AK47
6 Replies
10. Shell Programming and Scripting
Hi .. anyone can you help me ?
i need to convert text below into multiple columns
interface; GigabitEthernet0/0/0/0
description; TRUNK_PE-D2-JT2-VPN_Gi0/0/0/0_TO_ME4-A-JKT-JT_4/1/1_1G
mtu 9212
negotiation auto
interface; GigabitEthernet0/0/0/0.11
description; tes
encapsulation;... (1 Reply)
Discussion started by: mad3linux
1 Replies
LEARN ABOUT MOJAVE
fields
fields(3pm) Perl Programmers Reference Guide fields(3pm)
NAME
fields - compile-time class fields
SYNOPSIS
{
package Foo;
use fields qw(foo bar _Foo_private);
sub new {
my Foo $self = shift;
unless (ref $self) {
$self = fields::new($self);
$self->{_Foo_private} = "this is Foo's secret";
}
$self->{foo} = 10;
$self->{bar} = 20;
return $self;
}
}
my $var = Foo->new;
$var->{foo} = 42;
# this will generate an error
$var->{zap} = 42;
# subclassing
{
package Bar;
use base 'Foo';
use fields qw(baz _Bar_private); # not shared with Foo
sub new {
my $class = shift;
my $self = fields::new($class);
$self->SUPER::new(); # init base fields
$self->{baz} = 10; # init own fields
$self->{_Bar_private} = "this is Bar's secret";
return $self;
}
}
DESCRIPTION
The "fields" pragma enables compile-time verified class fields.
NOTE: The current implementation keeps the declared fields in the %FIELDS hash of the calling package, but this may change in future
versions. Do not update the %FIELDS hash directly, because it must be created at compile-time for it to be fully useful, as is done by
this pragma.
Only valid for perl before 5.9.0:
If a typed lexical variable holding a reference is used to access a hash element and a package with the same name as the type has declared
class fields using this pragma, then the operation is turned into an array access at compile time.
The related "base" pragma will combine fields from base classes and any fields declared using the "fields" pragma. This enables field
inheritance to work properly.
Field names that start with an underscore character are made private to the class and are not visible to subclasses. Inherited fields can
be overridden but will generate a warning if used together with the "-w" switch.
Only valid for perls before 5.9.0:
The effect of all this is that you can have objects with named fields which are as compact and as fast arrays to access. This only works as
long as the objects are accessed through properly typed variables. If the objects are not typed, access is only checked at run time.
The following functions are supported:
new perl before 5.9.0: fields::new() creates and blesses a pseudo-hash comprised of the fields declared using the "fields" pragma into
the specified class.
perl 5.9.0 and higher: fields::new() creates and blesses a restricted-hash comprised of the fields declared using the "fields" pragma
into the specified class.
This function is usable with or without pseudo-hashes. It is the recommended way to construct a fields-based object.
This makes it possible to write a constructor like this:
package Critter::Sounds;
use fields qw(cat dog bird);
sub new {
my $self = shift;
$self = fields::new($self) unless ref $self;
$self->{cat} = 'meow'; # scalar element
@$self{'dog','bird'} = ('bark','tweet'); # slice
return $self;
}
phash
before perl 5.9.0:
fields::phash() can be used to create and initialize a plain (unblessed) pseudo-hash. This function should always be used instead of
creating pseudo-hashes directly.
If the first argument is a reference to an array, the pseudo-hash will be created with keys from that array. If a second argument is
supplied, it must also be a reference to an array whose elements will be used as the values. If the second array contains less
elements than the first, the trailing elements of the pseudo-hash will not be initialized. This makes it particularly useful for
creating a pseudo-hash from subroutine arguments:
sub dogtag {
my $tag = fields::phash([qw(name rank ser_num)], [@_]);
}
fields::phash() also accepts a list of key-value pairs that will be used to construct the pseudo hash. Examples:
my $tag = fields::phash(name => "Joe",
rank => "captain",
ser_num => 42);
my $pseudohash = fields::phash(%args);
perl 5.9.0 and higher:
Pseudo-hashes have been removed from Perl as of 5.10. Consider using restricted hashes or fields::new() instead. Using
fields::phash() will cause an error.
SEE ALSO
base
perl v5.18.2 2014-01-06 fields(3pm)