I have a file in which I need to add more columns to based on a key in the first file:
File1
File2
I would like to create the following output:
Output
I'm trying to program this in Perl. My first thought is the following:
1. Fill an array with the keys in file1
2. open file2 and loop through the array on each row until i get a match then put the columns I want into a Hash of Arrays
3. Go back to the first file and apply the columns that i need to the end of the file.
However, the data sets that I'm working with are extremely big, and time is definitely an issue. Also, I think that my logic above is not the best way of doing it.
How do I add 4 columns to an excel file using Perl? The 4 headers for those columns will all have different names? Please help and I greatly appreciate... (1 Reply)
Hi Wise UNIX Crew,
I want to add 3 different columns to the file in which:
1. The first new column pulls in today's date and time
2. Second column one has a '0'
3. Third column has the word 'ANY' going down the column
If my file content is as follows:
"7","a","abc",123"... (1 Reply)
I want to select the first column from a daily file called foo.csv. The result is written to file foo.txt. Currently the following script is used for that:
cut -d, -f 1 foo.csv > foo.txt
A typical result would yield :
A12
A45
B11
B67
What needs to happen in addition is that two columns... (5 Replies)
Hi everybody,
I've got two simples file1 like:
aaa aaa aaa
bbb bbb bbb
ccc ccc ccc
and file2 like:
111 111 111
222 222 222
333 333 333
I need to:
1) add a line say "new line" as the first line of the file
2)add a column from file2 (say column3) to file1; the new column should... (14 Replies)
Good afternoon to everyone,
I have some input and output from various widgets that I am trying to get to play nicely together. Basically I would like to stay out of excel and be able to automate the entire process. I have read some posts here about how to use awk, nawk, etc, to do similar... (9 Replies)
Hi All ,
Kindly help me with this soln
awk '{printf "%s %7s \n", $1,$c}' infile
where
value of variable c I am externally giving input
But executing the above command shows all the columns of infile where as I want only 1st column of infile and 2nd column should print value c (8 Replies)
Hello,
I have a comma separated flat file. It contains some 20 columns. I want to add two new columns at position 2,3. So that file will have 22 columns. I am providing here sample data with file having 4 columns. Appreciate your help in finding solution for this.
data in input file:... (11 Replies)
Hi Everyone:
My shell script creates multiple csv files (~30) in for loop. I want to compile (or merge) 3rd column from each (all) of these files to another file (in loop). Please help. Thanks. (3 Replies)
Hello,
I am very now to this, hope you can help,
I am looking into editing a file in Solaris, with dinamic collums (lenght varies) and I need 2 things to be made, the fist is to filter the first column and third column from the file bellow file.txt, and create a new file with the 2 filtered... (8 Replies)
Discussion started by: jpbastos
8 Replies
LEARN ABOUT DEBIAN
data::optlist
Data::OptList(3pm) User Contributed Perl Documentation Data::OptList(3pm)NAME
Data::OptList - parse and validate simple name/value option pairs
VERSION
version 0.107
SYNOPSIS
use Data::OptList;
my $options = Data::OptList::mkopt([
qw(key1 key2 key3 key4),
key5 => { ... },
key6 => [ ... ],
key7 => sub { ... },
key8 => { ... },
key8 => [ ... ],
]);
...is the same thing, more or less, as:
my $options = [
[ key1 => undef, ],
[ key2 => undef, ],
[ key3 => undef, ],
[ key4 => undef, ],
[ key5 => { ... }, ],
[ key6 => [ ... ], ],
[ key7 => sub { ... }, ],
[ key8 => { ... }, ],
[ key8 => [ ... ], ],
]);
DESCRIPTION
Hashes are great for storing named data, but if you want more than one entry for a name, you have to use a list of pairs. Even then, this
is really boring to write:
$values = [
foo => undef,
bar => undef,
baz => undef,
xyz => { ... },
];
Just look at all those undefs! Don't worry, we can get rid of those:
$values = [
map { $_ => undef } qw(foo bar baz),
xyz => { ... },
];
Aaaauuugh! We've saved a little typing, but now it requires thought to read, and thinking is even worse than typing... and it's got a bug!
It looked right, didn't it? Well, the "xyz => { ... }" gets consumed by the map, and we don't get the data we wanted.
With Data::OptList, you can do this instead:
$values = Data::OptList::mkopt([
qw(foo bar baz),
xyz => { ... },
]);
This works by assuming that any defined scalar is a name and any reference following a name is its value.
FUNCTIONS
mkopt
my $opt_list = Data::OptList::mkopt($input, \%arg);
Valid arguments are:
moniker - a word used in errors to describe the opt list; encouraged
require_unique - if true, no name may appear more than once
must_be - types to which opt list values are limited (described below)
name_test - a coderef used to test whether a value can be a name
(described below, but you probably don't want this)
This produces an array of arrays; the inner arrays are name/value pairs. Values will be either "undef" or a reference.
Positional parameters may be used for compatibility with the old "mkopt" interface:
my $opt_list = Data::OptList::mkopt($input, $moniker, $req_uni, $must_be);
Valid values for $input:
undef -> []
hashref -> [ [ key1 => value1 ] ... ] # non-ref values become undef
arrayref -> every name followed by a non-name becomes a pair: [ name => ref ]
every name followed by undef becomes a pair: [ name => undef ]
otherwise, it becomes [ name => undef ] like so:
[ "a", "b", [ 1, 2 ] ] -> [ [ a => undef ], [ b => [ 1, 2 ] ] ]
By default, a name is any defined non-reference. The "name_test" parameter can be a code ref that tests whether the argument passed it is
a name or not. This should be used rarely. Interactions between "require_unique" and "name_test" are not yet particularly elegant, as
"require_unique" just tests string equality. This may change.
The "must_be" parameter is either a scalar or array of scalars; it defines what kind(s) of refs may be values. If an invalid value is
found, an exception is thrown. If no value is passed for this argument, any reference is valid. If "must_be" specifies that values must
be CODE, HASH, ARRAY, or SCALAR, then Params::Util is used to check whether the given value can provide that interface. Otherwise, it
checks that the given value is an object of the kind.
In other words:
[ qw(SCALAR HASH Object::Known) ]
Means:
_SCALAR0($value) or _HASH($value) or _INSTANCE($value, 'Object::Known')
mkopt_hash
my $opt_hash = Data::OptList::mkopt_hash($input, $moniker, $must_be);
Given valid "mkopt" input, this routine returns a reference to a hash. It will throw an exception if any name has more than one value.
EXPORTS
Both "mkopt" and "mkopt_hash" may be exported on request.
AUTHOR
Ricardo Signes <rjbs@cpan.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2006 by Ricardo Signes.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
perl v5.12.3 2011-05-12 Data::OptList(3pm)