cut the first row first column value

Hi All,

file create2.txt contains

. /test.txt
./create.txt
./test2.txt

test.txt contains

inb-1|kuuhsdkjfhsd|djshfjksd|ndsbs896
inb-2|kuuhsdkjfhsd|djshfjksd|ndsbs896
inb-3|kuuhsdkjfhsd|djshfjksd|ndsbs896

create.txt contains

abv-1|kjsdgfjksd|sdkjbfhokjsd|skjdbfhjkosd
abv-2|kjsdgfjksd|sdkjbfhokjsd|skjdbfhjkosd
abv-3|kjsdgfjksd|sdkjbfhokjsd|skjdbfhjkosd

test2.txt contains

nhg-1|kgsdjhfgsd|kdgojhgdfog|jhfgojhgdfljg
nhg-2|kgsdjhfgsd|kdgojhgdfog|jhfgojhgdfljg
nhg-3|kgsdjhfgsd|kdgojhgdfog|jhfgojhgdfljg

Kindly give me a perl script for the following output

inb-1
abv-1
nhg-1

It means i want to cut the first row first coloum of the mentioned file in create2.txt

anyone can help me out

One way:

$ cat filter.pl
#!/usr/bin/perl
 
use strict;
use warnings;
 
open (F, "<create2.txt" ) || die "unable to open file: $!";
while ( my $file=<F> ) {
        chomp($file);
        open (FF, "<$file" ) || die "unable to open file: $!";
        while ( my $line=<FF> ) {
                last if $. == 2;
                my @fields=split('\|',$line);
                print "$fields[0]\n";
        }
        close FF;
}
close F;
$ 
$ 
$ 
$ 
$ ./filter.pl 
inb-1
abv-1
nhg-1
$ 

Btw, No harm in continuing the original post.

getting the below error

Bareword found where operator expected at filter.pl line 1, near "$ cat filter"
(Missing operator before filter?)
syntax error at filter.pl line 1, near "$ cat filter"
"use" not allowed in expression at filter.pl line 4, at end of line
BEGIN not safe after errors--compilation aborted at filter.pl line 5.

Post your code. (copy and paste )

$ cat filter.pl
#!/usr/bin/perl
 
use strict;
use warnings;
 
open (F, "<create2.txt" ) || die "unable to open file: $!";
while ( my $file=<F> ) {
        chomp($file);
        open (FF, "<$file" ) || die "unable to open file: $!";
        while ( my $line=<FF> ) {
                last if $. == 2;
                my @fields=split('\|',$line);
                print "$fields[0]\n";
        }
        close FF;
}
close F;
$ 
$ 
$ 
$ 
$ ./filter.pl 
inb-1
abv-1
nhg-1

$

I guess, You have put everything in the file?

If filter.pl is your script name, then it should contain this:

#!/usr/bin/perl
 
use strict;
use warnings;
 
open (F, "<create2.txt" ) || die "unable to open file: $!";
while ( my $file=<F> ) {
        chomp($file);
        open (FF, "<$file" ) || die "unable to open file: $!";
        while ( my $line=<FF> ) {
                last if $. == 2;
                my @fields=split('\|',$line);
                print "$fields[0]\n";
        }
        close FF;
}
close F;

Also, if your perl path is differ from "/usr/bin/perl" replace it.

#!/bin/bash
head -q -n1 $(<create2.txt) | cut -d'|' -f1

(also works with ksh93),
or

#!/bin/sh
head -q -n1 `cat create2.txt` | cut -d'|' -f1

Hi its working fine. thanks a lot, i just want to save this out with testing.txt, how come we do that.

$ ./filter.pl > testing.txt