The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl Search and replace entire line insania Shell Programming and Scripting 1 05-22-2008 03:45 PM
Search and replace in Perl jyoung Shell Programming and Scripting 2 04-22-2008 10:05 AM
Perl: Search for string on line then search and replace text Crypto Shell Programming and Scripting 4 01-04-2008 06:24 AM
perl search and replace pairs umen Shell Programming and Scripting 1 07-30-2006 08:37 AM
Global search ok...but replace? alan UNIX for Dummies Questions & Answers 0 03-25-2004 12:22 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-06-2008
Registered User
 

Join Date: May 2008
Posts: 7
Stumble this Post!
Cool Perl: Global Search and replace

I have a file where the rows correspond to individuals and the columns are about 106 variables. Each variable is coded as either ACGT, and "missing" is coded as blank. This is a tab delimited file. I'm trying to replace all blanks (" ") with 0. The simple script I have is only replacing some of the blanks. Please help me figure out what's wrong with the script, so that every blank could be replaced by zero. Thanks!!!!

#!/usr/local/bin/perl
print "What file would you like to edit?";
$filename= <STDIN>;
open (TEXT, "$filename") || die "Can't open";
open (OUT, ">gd_53d.out" || die "Can't Open");
$var = " ";
while ($var ne "")
{
$var = <TEXT>;
if ($var eq "\t\t")
{
print OUT "\t\t";
next;
}
$var =~s/\t\t/\t0\t/gi;
print OUT $var;
}
close TEXT;
close OUT;
exit;
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-06-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,846
Stumble this Post!
You can't use sed?
Code:
sed 's/" "/0/g' file > newfile
Reply With Quote
  #3 (permalink)  
Old 05-06-2008
Registered User
 

Join Date: May 2008
Posts: 7
Stumble this Post!
Smile

Quote:
Originally Posted by jim mcnamara View Post
You can't use sed?
Code:
sed 's/" "/0/g' file > newfile
doesn't work.

example format of file:

id a1 a2
1 A B
2 A B
3
4 A B


Even the following perl code only replaces some of the blanks with 0.

#!/usr/local/bin/perl
open (TEXT, "gd_53.txt" || die "Can't open");
open (OUT, ">gd_53b.out" || die "Can't Open");
undef($/);
$var = <TEXT>;
$var =~ s/\t\t/\t0\t/g;
print OUT $var;
exit;

thanks!
Reply With Quote
  #4 (permalink)  
Old 05-06-2008
Registered User
 

Join Date: Jan 2008
Posts: 308
Stumble this Post!
Ae you sure the delimiter is tabs? Try \s+ instead of \t:

/\s+/[-0-]/g

the [- -] is just visual stuff to help you see what is happening, that can be removed after you get the substitution working properly.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:53 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0