Perl - Extract first column from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl - Extract first column from file
# 1  
Old 10-30-2014
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.
Code:
perl -anle 'print $F[0]' Input.dat > Output.dat
perl -ne '@F = split("\t", $_); print "$F[0]\n";' Input.dat > Output.dat
perl -anE 'say "@F[0,1]"' Input.dat > Output.dat

But when I add this in perl script it is not working.
Code:
	$cmd = "perl -anle 'print $F[0]' ".$inFile." > ".$outFile;
	system($cmd);
	if ($? != 0)
	{
		errorLog("Could not create file ".$inFile);
	}


Regards
Neethu
# 2  
Old 10-30-2014
You are already inside Perl. There is no point running system() -- which runs a Bourne shell -- to run Perl in commandline again inside a Bourne shell.

It won't quite be a "one-liner" anymore, but it will still be short, and it will also be better-checked for errors.

Code:
open(OUTFILE, ">Output.dat") || die("Couldn't open Iutput.dat");
open(INFILE, "<Input.dat") || die("Couldn't open Input.dat");
        while(@F = split("\t", <INFILE>)) { print OUTFILE "$F[0]\n" }
close(OUTFILE); close(INFILE);

# 3  
Old 10-30-2014
try

Code:
#!/usr/bin/perl

use strict;
use warnings;

open(IN, "in.txt") or die $!;
open(OUT, ">out.txt") or die $!;

while( <IN>)
{
	my @C =split( /\s+/, $_ );
	print OUT "$C[0]\n";
}

close (IN);
close (OUT);

# 4  
Old 10-30-2014
Quote:
Originally Posted by Neethu
[...]
But when I add this in perl script it is not working.
Code:
	$cmd = "perl -anle 'print $F[0]' ".$inFile." > ".$outFile;
	system($cmd);
	if ($? != 0)
	{
		errorLog("Could not create file ".$inFile);
	}

Neethu
Using the system() function to execute Perl code inside a Perl script is like using a wheelchair on someone that does not have problems running. Don't let a few more lines of code fool you.
Code:
open(IN, "in.txt") or die $!;
open(OUT, ">out.txt") or die $!;

Code:
open(OUTFILE, ">Output.dat") || die("Couldn't open Iutput.dat");
open(INFILE, "<Input.dat") || die("Couldn't open Input.dat");

Long time ago, creating files handles in that way was OK in Perl, until other problems appeared with not so good consequences.

Today, it is considered good practice to use lexical variables for filehandles and separate the read or write operator from the filename or filename variable, establishing the rule of thumb of always using a three-arguments to open. And I would like to promote that.

e.i.

Code:
#!/usr/bin/perl

use strict;
use warnings;

my $inFile  = "Input.dat";
my $outFile = "Output.dat";

open my $fh_in,  "<", "$inFile"  or die "Could not open $inFile: $!\n";
open my $fh_out, ">", "$outFile" or die "Could not create $outFile: $!\n";

while ( my $line = <$fh_in> ) {
    my $first_column = (split /\s+/, $line)[0];
    print $fh_out "$first_column\n";
}
close $fh_in;
close $fh_out;


Last edited by Aia; 10-30-2014 at 01:48 PM.. Reason: Align code
This User Gave Thanks to Aia For This Post:
# 5  
Old 10-31-2014
Thank you all for the reply.
I tried the below code. The file is a tab delimited file. If there is a null value in first column in the third row then it is pulling the second column from that row and saving it in the output file.

Code:
#!/usr/bin/perl

use strict;
use warnings;

my $inFile  = "Input.dat";
my $outFile = "Output.dat";

open my $fh_in,  "<", "$inFile"  or die "Could not open $inFile: $!\n";
open my $fh_out, ">", "$outFile" or die "Could not create $outFile: $!\n";

while ( my $line = <$fh_in> ) {
    my $first_column = (split /\t+/, $line)[0];
    print $fh_out "$first_column\n";
}
close $fh_in;
close $fh_out;

Input File:
111 123 456
222 234 567
null 345 678

Output file from above code:
111
222
345

Expected Output:
111
222
# 6  
Old 10-31-2014
In that case it is not tab delimited, use the split/\s+/ as outlined in Aia's post aboce
# 7  
Old 10-31-2014
This is a bit more terse:

Code:
open(OUTFILE, ">Output.dat") || die("Couldn't open Iutput.dat");
open(INFILE, "<Input.dat") || die("Couldn't open Input.dat");
        while(@F = split(/\s+/, <INFILE>)) { print OUTFILE "$F[0]\n" }
close(OUTFILE); close(INFILE);

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

I need extract column pattern in file

Hi, it's my first time in this site. I've a file that look likes Edges 21 82 Edges 3 22 Edges 34 12 Edges 1 24 Edges 6 2 Edges 12 22 etc. I need extract just the second and third column with the space between them. Thanks:) Please use code tags next time for your code and data. (4 Replies)
Discussion started by: iMunk
4 Replies

3. 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

4. Shell Programming and Scripting

Extract column to a new file

Hi All, Using below command to extract text from a file grep -E "^.{20}5004" filename.rtf >> 5004This will give all lines with text 5004 starting at position 20. The file filename.rtf contains several rows (millions). The four characters starting from 20 position is repeating in several... (4 Replies)
Discussion started by: hsehdar
4 Replies

5. UNIX for Dummies Questions & Answers

How to extract one column from csv file in perl?

Hi everyone, i am new to perl programming, i have a problem in extracting single column from csv file. the column is the 20th column, please help me.. at present i use this code #!C:/perl/bin use warnings; use strict; my $file1 = $ARGV; open FILE1, "<$file1" or die "Can't... (13 Replies)
Discussion started by: kvth
13 Replies

6. 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

7. Shell Programming and Scripting

To extract last column of file

Hi, I need to extract last column of each row of a file (may be 'cut' should do). And I don't know the number of last column. (2 Replies)
Discussion started by: DivyaG
2 Replies

8. 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

9. Shell Programming and Scripting

How to extract only first column from the file

Dear All, I have a file name pointer.unl. It's contains the information below: O|A|4560333089|PBS|AU1|01/04/2003|30/04/2006|D|IGCD| O|A|4562222089|PBN|AU1|01/02/2006|31/01/2008|D|04065432| O|A|3454d00089|PKR|AU1|01/03/2008||R|sdcdc| I only need to extract first... (11 Replies)
Discussion started by: selamba_warrior
11 Replies

10. UNIX for Dummies Questions & Answers

Extract column data from File

I have a file containing the lines similar to the following entries: File1.txt: ..... -rw-r--r-- 1 root staff 4110 Aug 7 17:02 XXX_OrderNum1_date1_time1.txt -rw-r--r-- 1 root staff 4110 Aug 7 17:02 XXX_OrderNum2_date2_time1.txt -rw-r--r-- 1 root staff ... (3 Replies)
Discussion started by: sudheshnaiyer
3 Replies
Login or Register to Ask a Question