Visit Our UNIX and Linux User Community

Extract first column from second line in perl

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract first column from second line in perl
# 1  
Old 09-07-2011
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

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

The records are in a flat file

Thanks for sparing your time to help me out
# 2  
Old 09-07-2011
perl -F'\|' -alne '$. == 2 && print $F[0]' INPUTFILE

This User Gave Thanks to yazu For This Post:
# 3  
Old 09-07-2011
perl -ne 'printf $.==2&& /^(.+?)\|/?"$1\n":"";' filename

Last edited by pravin27; 09-07-2011 at 02:28 AM..
This User Gave Thanks to pravin27 For This Post:
# 4  
Old 09-07-2011
$ perl -F"\|" -ane 'print "$F[0]\n" if($.==2)' test

$ cat test
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

This User Gave Thanks to itkamaraj For This Post:
# 5  
Old 09-07-2011
Try this,
perl -F"\|" -ane 'print "$F[0]\n" if($.==2)' input_file


Last edited by Franklin52; 09-07-2011 at 04:34 AM.. Reason: Please use code tags for code and data samples, thank you
This User Gave Thanks to rangarasan For This Post:
# 6  
Old 09-07-2011
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

No luck

Please help
# 7  
Old 09-07-2011
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.
This User Gave Thanks to yazu For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #81
Difficulty: Easy
AT&T UNIX, SCO UnixWare, Sun Microsystems Solaris, HP-UX, IBM AIX, SGI IRIX and many big corporations who wanted license and profit from their own versions of Unix are not as common today as they were in the 1980s.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl to extract information from a file line by line

In the below perl code I am using tags within each line to extract certain information. The tags that are used are: STB >0.8 is STRAND BIAS otherwise GOOD FDP is the second number GO towards the end of the line is read into an array and the value returned is outputed, in the first line that... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. UNIX for Dummies Questions & Answers

Extract columns based on the first line of each column

Sorry to bother you guys again. I have a file1 with multiple columns like this:gga_miR_100 gga_miR_300 gga_miR_3500 gga_miR_4600 gga_miR_5600 gga_miR_30 gga_miR_500 kj rwg ghhh jy jyu we vf 5g 5hg h6 56h i8 45t 44r4 4bg 4r546 9lgtr (fer) 4fr f433 3feev f4 bf4 35g vfr ge 2rr ... (5 Replies)
Discussion started by: yuejian
5 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. UNIX for Dummies Questions & Answers

Compare two column in file and extract complate line

No Chr Pos Qual GT_1 GT_2 1. chr1 478493 595 A/G G/G 2. chr1 879243 700 A/T T/T 3. chr2 889922 1300 C/C C/C 4. chr2 1926372 300 T/A T/A 5. chr3 237474 500 G/C C/C 6. chr3 575757 700 ... (2 Replies)
Discussion started by: mscott
2 Replies

6. Shell Programming and Scripting

Extract Line and Column from CSV Line in ksh or bash format

Hi, I was doing some research and can't seem to find anything. I'm trying to automate a process by creating a script to read a csv line and column and assigning that value to a variable for the script to process it. Also if you could tell me the line and column if it's on another work ... (3 Replies)
Discussion started by: vpundit
3 Replies

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

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

10. Shell Programming and Scripting

How to extract 3rd line 4th column of a file

Hi, Shell script: I would need help on How to extract 3rd line 4th column of a file with single liner Thanks in advance. (4 Replies)
Discussion started by: krishnamurthig
4 Replies

Featured Tech Videos