The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 06:45 PM
Search and replace in Perl jyoung Shell Programming and Scripting 2 04-22-2008 01:05 PM
Perl: Search for string on line then search and replace text Crypto Shell Programming and Scripting 4 01-04-2008 10:24 AM
perl search and replace pairs umen Shell Programming and Scripting 1 07-30-2006 11:37 AM
Global search ok...but replace? alan UNIX for Dummies Questions & Answers 0 03-25-2004 04:22 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-06-2008
epi8 epi8 is offline
Registered User
  
 

Join Date: May 2008
Posts: 7
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;
  #2 (permalink)  
Old 05-06-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
You can't use sed?
Code:
sed 's/" "/0/g' file > newfile
  #3 (permalink)  
Old 05-06-2008
epi8 epi8 is offline
Registered User
  
 

Join Date: May 2008
Posts: 7
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!
  #4 (permalink)  
Old 05-06-2008
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:12 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0