How to extract one column from csv file in perl?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to extract one column from csv file in perl?
# 8  
Old 11-29-2010
Code:
my $outputfile = $file . out;
open (OUT, ">", $outputfile);
while(<CSV>)
{
my @line = split(/\t/, $_);
$line[19] = your_change;
foreach(@line)
{
print "$_\t";
print OUT "$_\t";
}
print "\n";
print OUT "\n";
}
close OUT;

# 9  
Old 11-29-2010
HOW to update one column from csv file

Hi,
i tried your code, but its working as infinte loop,, so the program didn't get executed properly...
# 10  
Old 11-29-2010
Can you post the whole program in code tags please?
# 11  
Old 11-30-2010
here is the entire code..!!

here is the entire code..what i am using right now..!!


Code:
 
#!C:/perl/bin
use warnings;
use strict;
 
my $file1 = $ARGV[0];
my @input = "";
my @line;
open FILE1, "<$file1"
    or die "Can't open $file1: $!\n";
 
     while(<FILE1>)
       
       my @line = split(/,/, $_);
       $line[19] = 10;
       foreach(@line)
       {
       print "$_\t";
       }
       print "\n";

# 12  
Old 11-30-2010
How about this:

Code:
 
#!C:/perl/bin
use warnings;
use strict;
 
my $file1 = $ARGV[0];
my @input = "";
my @line;
my $outfile = $file1 . updated
open FILE1, "<$file1"
    or die "Can't open $file1: $!\n";

open OUT, "> $outfile"
    or die "Can't open $outfile: $!\n;
 
     while(<FILE1>)
       {
       my @line = split(/\t/, $_);
       $line[19] = 10;
       foreach(@line)
       {
         print "$_\t";
         print OUT "$_\t";
       }
       print "\n";
       print OUT "\n";
       }

close FILE1;
close OUT;

# 13  
Old 11-30-2010
Hey i am getting the following errors...

Quote:
"my" variable @line masks earlier declaration in same scope at C:\Documents and
Settings\kavitha.n\Week_new.pl line 17.

syntax error at C:\Documents and Settings\kavitha.n\Week_new.pl line 9, near "up
dated
open "

please help...!!
# 14  
Old 11-30-2010
Code:
 
#!C:/perl/bin
use warnings;
use strict;
 
my $file1 = $ARGV[0];
my @input = "";
my @line;
my $outfile = $file1 . updated
open FILE1, "<$file1"
    or die "Can't open $file1: $!\n";

open OUT, "> $outfile"
    or die "Can't open $outfile: $!\n;
 
     while(<FILE1>)
       {
       @line = split(/\t/, $_);
       $line[19] = 10;
       foreach(@line)
       {
         print "$_\t";
         print OUT "$_\t";
       }
       print "\n";
       print OUT "\n";
       }

close FILE1;
close OUT;

This User Gave Thanks to ilikecows For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I extract specific column in multiple csv files?

file1: Name,Threshold,Curr Samples,Curr Error%,Curr ART GETHome,100,21601,0.00%,47 GETregistry,100,21592,0.00%,13 GEThomeLayout,100,30466,0.00%,17 file2: Name,Threshold,Curr Samples,Curr Error%,Curr ART GETHome,100,21601,0.00%,33 GETregistry,100,21592,0.00%,22... (6 Replies)
Discussion started by: Raghuram717
6 Replies

2. Shell Programming and Scripting

Compare 2 files of csv file and match column data and create a new csv file of them

Hi, I am newbie in shell script. I need your help to solve my problem. Firstly, I have 2 files of csv and i want to compare of the contents then the output will be written in a new csv file. File1: SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0713.JPG,2015:02:17 11:14:07... (8 Replies)
Discussion started by: refrain
8 Replies

3. Shell Programming and Scripting

Perl - Extract first column from file

Hi, I want to extract first column from a file and redirect the output to another file in perl. I am able to do this from command line by executing below commands. perl -anle 'print $F' Input.dat > Output.dat perl -ne '@F = split("\t", $_); print "$F\n";' Input.dat > Output.dat perl -anE... (7 Replies)
Discussion started by: Neethu
7 Replies

4. Shell Programming and Scripting

Perl regexp to extract first and second column

Hi, I am trying with the below Perl one-liner using regular expression to extract the first and second column of a text file: perl -p -e "s/\s*(\w+).*/$1/" perl -p -e "s/\s*.+\s(.+)\s*/$1\n/" whereas the text file's data looks like: Error: terminated 2233 Warning: reboot 3434 Warning:... (3 Replies)
Discussion started by: royalibrahim
3 Replies

5. Shell Programming and Scripting

Perl code to grep a particular column in CSV format

Hi I want to grep a column 6 & column 7 from a CSV Format file & then i have to find the difference between these columns as these both columns contains date & time in 7/7/2012 9:20 this format . So kindly help me out ASAP. But please kindly dis xls has to be converted in csv format as may... (5 Replies)
Discussion started by: Prateek@123
5 Replies

6. Shell Programming and Scripting

3 column .csv --> correlation matrix; awk, perl?

Greetings, salutations. I have a 3 column csv file with ~13 million rows and I would like to generate a correlation matrix. Interestingly, you all previously provided a solution to the inverse of this problem. Thread title: "awk? adjacency matrix to adjacency list / correlation matrix to list"... (6 Replies)
Discussion started by: R3353
6 Replies

7. Shell Programming and Scripting

Extract first column from second line in perl

Hello Gurus I have a source file which has the first line as header and the rest are the records I need to extract the first column from the second line to extract a value I/P ... (7 Replies)
Discussion started by: Pratik4891
7 Replies

8. Shell Programming and Scripting

Perl script to extract second column from a xls

Can Anyone tell me how to extract the second column of a xls sheet And compare the content of each row of the column with a .h file. xls sheet is having only one spreadsheet. (2 Replies)
Discussion started by: suvenduperl
2 Replies

9. Shell Programming and Scripting

extract csv based on column value

Hi I have a csv file which is below A,5 B,6 C,10 D,7 I want the values who's second column is greater than 7 say C,10 D,7 Help me please... Thanks, Maruth (3 Replies)
Discussion started by: maruthavanan
3 Replies

10. Shell Programming and Scripting

extract values from column with Perl

Hi everybody I have some problems with PERL programming. I have a file with two columns, both with numeric values. I have to extract the values > 50 from the 2nd columns and sum them among them. The I have to sum the respective values in the first column on the same line and, at the end, I... (6 Replies)
Discussion started by: m_elena
6 Replies
Login or Register to Ask a Question