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

 
#DATA_ACQ_CYC_CNTL_ID|DATA_ACQ_CYC_CNTL_SNPSHT_DT|CONCAT_KEY|FCTR_LVL_CD|RNWL_NS_CD|RTG_PLN_YR_CD|CMBN_DED_UNT_CD|DEDUCT_AMT|REIM_AMT|EFCTV_DT|EE_PRA_RT|SP_PRA_RT|CH_PRA_RT|TRMNTN_DT|UPDT_SEQ_NMBR|LST_TRNS_DT|LST_TRNS_USR_ID|MAX_OFST_AMT
1783579|090511|0000000000000010006100001001500005020071001|0|1|61|1|150|50|10/01/2007 00:00:00|2.33|2.43|2.36|12/31/2599 00:00:00|0|06/13/2007 00:00:00|G000854 |90
1783579|090511|0000000000000010006100001003000010020071001|0|1|61|1|300|100|10/01/2007 00:00:00|4.42|4.62|4.28|12/31/2599 00:00:00|0|06/13/2007 00:00:00|G000854 |190

O/P should be

 
1783579

The records are in a flat file

Thanks for sparing your time to help me out

perl -F'\|' -alne '$. == 2 && print $F[0]' INPUTFILE
1 Like
perl -ne 'printf $.==2&& /^(.+?)\|/?"$1\n":"";' filename
1 Like
 
$ perl -F"\|" -ane 'print "$F[0]\n" if($.==2)' test
1783579

$ cat test
#DATA_ACQ_CYC_CNTL_ID|DATA_ACQ_CYC_CNTL_SNPSHT_DT|CONCAT_KEY|FCTR_LVL_CD|RNWL_NS_CD|RTG_PLN_YR_CD|CMBN_DED_UNT_CD|DEDUCT_AMT|REIM_AMT|EFCTV_DT|EE_PRA_RT|SP_PRA_RT|CH_PRA_RT|TRMNTN_DT|UPDT_SEQ_NMBR|LST_TRNS_DT|LST_TRNS_USR_ID|MAX_OFST_AMT
1783579|090511|0000000000000010006100001001500005020071001|0|1|61|1|150|50|10/01/2007 00:00:00|2.33|2.43|2.36|12/31/2599 00:00:00|0|06/13/2007 00:00:00|G000854 |90
test|090511|0000000000000010006100001003000010020071001|0|1|61|1|300|100|10/01/2007 00:00:00|4.42|4.62|4.28|12/31/2599 00:00:00|0|06/13/2007 00:00:00|G000854 |190

1 Like

Hi,
Try this,

perl -F"\|" -ane 'print "$F[0]\n" if($.==2)' input_file

Cheers,
Ranga:-)

1 Like

Thanks a lot all of you guys

But I have to open the file from a script

Currently the code is below

 
open my $fd, "<", $file;
my $a = (split /\|/, scalar <$fd>)[0];
close $fd;

Thanks to Yazu

But how can I add the second line first column condition in the above code

I tried with

 
if($.==2)

No luck

Please help

open my $fd, "<", $file;
<$fd>; # skip the first line;
my $a = (split /\|/, scalar <$fd>)[0];
close $fd;

If you need some other processing you need read the whole line into an array or read lines in a loop.

1 Like

Thanks a lot ..working perfect now