# Perl - Extract first column from file

**URL:** <https://community.unix.com/t/perl-extract-first-column-from-file/347651>\
**Category:** Shell Programming and Scripting\
**Created:** [October 30, 2014, 10:41am UTC](https://community.unix.com/t/perl-extract-first-column-from-file/347651 "2014-10-30T10:41:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neethu](https://community.unix.com/letter_avatar/neethu/32/5_5575768a8748004e209b776fc1b2916d.png) [@Neethu](https://community.unix.com/u/Neethu)\
**Post date:** [October 30, 2014, 10:41am UTC](https://community.unix.com/t/perl-extract-first-column-from-file/347651/1 "2014-10-30T10:41:05Z")

</div>

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.

```nohighlight
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.

```nohighlight
	$cmd = "perl -anle 'print $F[0]' ".$inFile." > ".$outFile;
	system($cmd);
	if ($? != 0)
	{
		errorLog("Could not create file ".$inFile);
	}

```

Regards  
Neethu

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [October 30, 2014, 11:36am UTC](https://community.unix.com/t/perl-extract-first-column-from-file/347651/2 "2014-10-30T11:36:41Z")

</div>

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.

```nohighlight
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);

```

---

<div class="post-metadata">

**Author:** ![Akshay\_Hegde](https://community.unix.com/user_avatar/community.unix.com/akshay_hegde/32/2771_2.png) [@Akshay\_Hegde](https://community.unix.com/u/Akshay_Hegde)\
**Post date:** [October 30, 2014, 11:46am UTC](https://community.unix.com/t/perl-extract-first-column-from-file/347651/3 "2014-10-30T11:46:06Z")

</div>

try

```nohighlight
#!/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);

```

---

<div class="post-metadata">

**Author:** ![Aia](https://community.unix.com/user_avatar/community.unix.com/aia/32/917_2.png) [@Aia](https://community.unix.com/u/Aia)\
**Post date:** [October 30, 2014, 12:29pm UTC](https://community.unix.com/t/perl-extract-first-column-from-file/347651/4 "2014-10-30T12:29:45Z")

</div>

> [@neethu](#):
>
> [...]  
> But when I add this in perl script it is not working.
> 
> ```plaintext
> $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.

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

```

```plaintext
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.

```plaintext
#!/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;

```

---

<div class="post-metadata">

**Author:** ![Neethu](https://community.unix.com/letter_avatar/neethu/32/5_5575768a8748004e209b776fc1b2916d.png) [@Neethu](https://community.unix.com/u/Neethu)\
**Post date:** [October 31, 2014, 6:08am UTC](https://community.unix.com/t/perl-extract-first-column-from-file/347651/5 "2014-10-31T06:08:47Z")

</div>

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.

```nohighlight
#!/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

---

<div class="post-metadata">

**Author:** ![Skrynesaver](https://community.unix.com/user_avatar/community.unix.com/skrynesaver/32/14143_2.png) [@Skrynesaver](https://community.unix.com/u/Skrynesaver)\
**Post date:** [October 31, 2014, 7:15am UTC](https://community.unix.com/t/perl-extract-first-column-from-file/347651/6 "2014-10-31T07:15:50Z")

</div>

In that case it is not tab delimited, use the `split/\s+/` as outlined in Aia's post aboce

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [October 31, 2014, 11:10am UTC](https://community.unix.com/t/perl-extract-first-column-from-file/347651/7 "2014-10-31T11:10:07Z")

</div>

This is a bit more terse:

```nohighlight
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);

```

---

<div class="post-metadata">

**Author:** ![Neethu](https://community.unix.com/letter_avatar/neethu/32/5_5575768a8748004e209b776fc1b2916d.png) [@Neethu](https://community.unix.com/u/Neethu)\
**Post date:** [November 3, 2014, 7:48am UTC](https://community.unix.com/t/perl-extract-first-column-from-file/347651/8 "2014-11-03T07:48:36Z")

</div>

Thank you all... It worked as expected..
