Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert text

Status
Not open for further replies.

noyk

MIS
Feb 20, 2009
23
MY
Hi, i'm trying to write a script to read this

YAL044W-A 0000004
YAL044W-A 0005554
YAL044W-A 0008372

and convert into this

YAL044W-A 0000004, 0005554, 0008372

unfortunately, this is as far as i get

open FH, "<infile.txt" or die $!;
open OF, ">outputfile.txt";
while (<FH>) {
($key, @values)=split /\s+/, $_;
print "$key \t";
foreach ($key) {
print OF "@values," ;
}
}

thanks for any help
 
Hi

For your sample input this is enough :
Code:
perl -pae 'chomp;s/^$p +/, /;$p=$F[0]' infile.txt > outputfile.txt
If it not gives what you want, you should give us more details about the input.

Feherke.
 
thanks.

my infile would be somethiing like this

3834_t: g3498.t1
3834_t: ghmm.1
3835_t: g3499.t1
3835_t: g3500.t1
3835_t: ghmm.2
3835_t: ghmm.3
3836_t: g3500.t1
3836_t: ghmm.3
3836_t: ghmm.4

your one-liner works fine, just that i will need each key on a new line.
 
Hi

You did not tried to change the code to fit your requirement, right ?
Code:
perl -pae 'chomp;[highlight]print"\n"if$p&&$p ne$F[0];[/highlight]s/^$p +/, /;$p=$F[0]' infile.txt > outputfile.txt

Feherke.
 
ok, thanks.

I was actually trying to do it the oppposite way

perl -pae 's/^$p +/, /;$p=$F[0]' infile.txt | sed "s/\n,/,/g" >outputfile.txt

doesnt work though.

btw, what is $F[0]' and -pae ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top