This is very similar to a post the other day, except that person wanted to keep the columns identified in the column file.
This should work for you:
I created two test files. The first, column.dat, with 21 lines containing 300,000 columns of data delimited by spaces with a header line consisting of column headers:
The code is dependent on the columns in column.lst and the column headers in column.dat are in the same order.
Here's the perl code:
Code:
#!/usr/bin/perl
use strict;
my @a_column;
my @a_delcol;
my @a_allcol;
my @a_outcol;
my $datcol;
my $outline;
my $line;
my $date_stamp;
my $i;
my $d;
$date_stamp = localtime time;
print "START: $date_stamp\n";
open COLFILE, "<column.lst"
or die "can't open file: $!";
$i=0;
@a_delcol = <COLFILE>;
close COLFILE
or die "can't close file: $!";
open DATFILE, "<column.dat"
or die "can't open file: $!";
$datcol = <DATFILE>;
chomp ($datcol);
close DATFILE
or die "can't close file: $!";
@a_allcol = (split ' ', $datcol);
$i=0;
$d=0;
while ( $a_allcol[$i] )
{
chomp ($a_allcol[$i]);
chomp ($a_delcol[$d]);
if ( $a_allcol[$i] ne $a_delcol[$d] )
{
push (@a_column, $i);
push (@a_outcol, "$a_allcol[$i]");
}
else
{
$d++;
}
$i++;
}
undef @a_allcol;
undef @a_delcol;
open DATFILE, "<column.dat"
or die "can't open file: $!";
open OUTFILE, ">column.out"
or die "can't open file: $!";
$outline = join(" ", @a_outcol);
print OUTFILE "$outline\n";
undef @a_outcol;
while($line = <DATFILE>)
{
chomp($line);
$outline = join(" ", (split ' ', $line) [@a_column]);
print OUTFILE "$outline\n";
}
close DATFILE
or die "can't close file: $!";
close OUTFILE
or die "can't close file: $!";
$date_stamp = localtime time;
print "END: $date_stamp\n";
Here's the timing of the script:
Code:
START: Tue Dec 1 15:00:40 2009
END: Tue Dec 1 15:00:50 2009
Hi Friends,
I want to delete specific columns from a file.
Say my file content is as follows:
"1","a","ww1",1234"
"2","b","wwr3","2222"
"3","c","erre","3333"
Now i want to delete the column 2 and 4 from this file.
That is I want the file content to be:
"1","ww1"
"2","wwr3"... (11 Replies)
Hi All,
Am working on perl script which should delete columns in existing CSV file.
If my file is :
AA,BB,CC,DD
00,11,22,33
00,55,66,77
00,99,88,21
AA,BB... are all my headers can come in any order (e.g AA,CC,BB...) and rest are values. I want to delete column CC...
Can anybody help... (2 Replies)
This should follow with my last post but I think it's better to start a new one. Now I have a list of numbers stored in pos.txt:
2
6
7
.
.
.
n
.
.
.
And I need to extract (2n-1, 2n) columns from matrix.txt:
ind1 A B C D E F G H I J K L M N O P Q R ...
ind2 B C D E F G H... (3 Replies)
I have a space delimited text file with 1,000,000+ columns and 100 rows. I want to delete columns 2 through 5 (2 and 5) included from the text file. How do I do that? Thanks. (3 Replies)
Hi All,
i have a .Csv file in the below format
startTime, endTime, delta, gName, rName, rNumber, m2239max, m2239min, m2239avg, m100016509avg, m100019240max, metric3min, m100019240avg, propValues
11-Mar-2012 00:00:00, 11-Mar-2012 00:05:00, 300.0, vma3550a, a-1_CPU Index<1>, 200237463, 0.0,... (9 Replies)
Hi group,
Can you please tell how to delete specific columns from a file.
I know something like
awk -F, '{ print $1" "$2" "15 }' input.txt > output.txt will delete all other columns. But this is in a way to copy some particular columns.
But is there any other way to select just some... (11 Replies)
Hi Friends,
I have come across some files where some of the columns don not have data.
Key, Data1,Data2,Data3,Data4,Data5
A,5,6,,10,,
A,3,4,,3,,
B,1,,4,5,,
B,2,,3,4,,
If we see the above data on Data5 column do not have any row got filled. So remove only that column(Here Data5) and... (4 Replies)
Hello,
I have a list of words separated by spaces I am trying to delete from a text file, and I could not figure out what is the best way to do this.
what I tried (does not work) :
delete="password key number verify"
arr=($delete)
for i in arr
{
sed "s/\<${arr}\>]*//g" in.txt
}
>... (5 Replies)