Column extraction in perl

V

vkrish7

Hello all,
I am very new to PERL and need some help here to get this going...

I have about 100 text files and each text file looks something like
this:-

V_or_I Lo_BIT
5330 1
5380 5
5390 6
5400 9
5410 11
5420 15

Now i need to grab only the first column starting with (5330, skip
"V_or_I" line) go till the end of the file(Say till 5420) and write
this to another file(say dataappend.txt). Then open another text file(
out of 100 text files) do the same( ie. start from second line of the
file which will be in number format similar to 5330 and grab the first
column and append to dataappend.txt... Can any1 please show me how to
do this on perl?

Hoping for a reply!

Thanks,
Vijay
 
B

Brian McCauley

I am very new to PERL and need some help here to get this going...

It's Perl not PERL - see FAQ.
I have about 100 text files and each text file looks something like
this:-

V_or_I Lo_BIT
5330 1
5380 5
5390 6
5400 9
5410 11
5420 15

Now i need to grab only the first column starting with (5330, skip
"V_or_I" line) go till the end of the file(Say till 5420) and write
this to another file(say dataappend.txt). Then open another text file(
out of 100 text files) do the same( ie. start from second line of the
file which will be in number format similar to 5330 and grab the first
column and append to dataappend.txt... Can any1 please show me how to
do this on perl?

Hoping for a reply!

What bits are you having trouble with? Show us what you've wrtten so
far. People here will not be inclined to help you if there's no
evidence that you are making any effort yourself.
 
T

Ted Zlatanov

I have about 100 text files and each text file looks something like
this:-

V_or_I Lo_BIT
5330 1
5380 5
5390 6
5400 9
5410 11
5420 15

Now i need to grab only the first column starting with (5330, skip
"V_or_I" line) go till the end of the file(Say till 5420) and write
this to another file(say dataappend.txt). Then open another text file(
out of 100 text files) do the same( ie. start from second line of the
file which will be in number format similar to 5330 and grab the first
column and append to dataappend.txt... Can any1 please show me how to
do this on perl?

You may want to consider using the `cut' utility to do this. It was
designed for this sort of task.

With Perl it's pretty simple:

perl -ane 'print $F[0], "\n"' FILE1 FILE2 FILE3
perl -ane 'print $F[1], "\n"' FILE1 FILE2 FILE3

these two commands will print the first and second column,
respectively, of the file you give them. If you know you're not
interested in the header lines, remove them.

perl -ane 'print $F[0], "\n"' FILE.data | grep -v V_or_I
perl -ane 'print $F[1], "\n"' FILE.data | grep -v V_or_I

Type 'perldoc perlrun' to learn what the -a and -n switches do.

Ted
 
X

Xicheng Jia

Hello all,
I am very new to PERL and need some help here to get this going...

I have about 100 text files and each text file looks something like
this:-

V_or_I Lo_BIT
5330 1
5380 5
5390 6
5400 9
5410 11
5420 15

Now i need to grab only the first column starting with (5330, skip
"V_or_I" line) go till the end of the file(Say till 5420) and write
this to another file(say dataappend.txt). Then open another text file(
out of 100 text files) do the same( ie. start from second line of the
file which will be in number format similar to 5330 and grab the first
column and append to dataappend.txt... Can any1 please show me how to
do this on perl?

perl -ln0777e 'print for /^(?!\A)\s*(\S+)/mg' file* > dataappend.txt

Xicheng
 
J

John W. Krahn

I have about 100 text files and each text file looks something like
this:-

V_or_I Lo_BIT
5330 1
5380 5
5390 6
5400 9
5410 11
5420 15

Now i need to grab only the first column starting with (5330, skip
"V_or_I" line) go till the end of the file(Say till 5420) and write
this to another file(say dataappend.txt). Then open another text file(
out of 100 text files) do the same( ie. start from second line of the
file which will be in number format similar to 5330 and grab the first
column and append to dataappend.txt... Can any1 please show me how to
do this on perl?


perl -lne'/^\s*(\d+)\s/ && print $1' *.txt > dataappend.txt



John
 
T

Ted Zlatanov

On 15 Sep 2006, (e-mail address removed) wrote:

perl -lne'/^\s*(\d+)\s/ && print $1' *.txt > dataappend.txt

This assumes numerics. The OP didn't specify that, and there's no
need to assume it to get the job done.

Ted
 
T

Ted Zlatanov

This assumes numerics. The OP didn't specify that,

Sorry. I thought I had read the OP's post carefully but I didn't,
missing the "number format" part. Never mind.
and there's no need to assume it to get the job done.

Ted
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,653
Latest member
YvonneJif

Latest Threads

Top