Perl: Global Search and replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: Global Search and replace
# 1  
Old 05-06-2008
Tools 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  
Old 05-06-2008
You can't use sed?
Code:
sed 's/" "/0/g' file > newfile

# 3  
Old 05-06-2008
Bug

Quote:
Originally Posted by jim mcnamara
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  
Old 05-06-2008
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search and replace (perl)

How can I achieve this? Perl would be awesome. Input string a_b c //Note there is a blank here Needed Output a_b_c Thanks (4 Replies)
Discussion started by: dragonpoint
4 Replies

2. Shell Programming and Scripting

Global replace with perl in bash

I am newbie to perl, I am trying to use the below syntax to replace globally a string with a variable. $ bash -version GNU bash, version 2.05b.0(1)-release (powerpc-ibm-aix5.1) Copyright (C) 2002 Free Software Foundation, Inc. $ perl -version This is perl, v5.8.8 built for... (2 Replies)
Discussion started by: jville
2 Replies

3. Shell Programming and Scripting

Global search and replace multi line file

Hello I need to search for a mult-line strngs(with spaces in between and qoted) in a file1 and replace that text with Fixed string globally in file1. The strng to search for is in file2. The file is big with some 20K records. so speed and effciency is required file1: (where srch & rplc... (0 Replies)
Discussion started by: Hiano
0 Replies

4. Shell Programming and Scripting

Perl - search and replace a particular field

Hi, I have a file having around 30 records. Each record has 5 fields delimited by PIPE. Few records in the file having Junk characters in the field2 and field4. I found the junk charcter and I tested it and replace the junk with space with the command below perl -i -p -e "s/\x00/ /g"... (1 Reply)
Discussion started by: ramkrix
1 Replies

5. Shell Programming and Scripting

Search and Replace in Perl

I am trying to write a simple perl script to run on a FreeBSD machine. There are alot of posts here, and I have read so many, yet can not get this script to run. #!/usr/bin/perl -e 's/\r\n/~/' infile.txt outfile.txt I am trying to take a windows text file, move it into Unix, run a script on... (1 Reply)
Discussion started by: mach1
1 Replies

6. Shell Programming and Scripting

Global search and replace across multiple files

Hi all I'm in need of a command which can replace a specified string with another string - across multiple files within multiple sub-directories (I intend to run it from / ) I've used the following to get a list of the files: find . | xargs grep <string1> But that's as far as I've got.... (7 Replies)
Discussion started by: huskie69
7 Replies

7. Shell Programming and Scripting

Search and replace in Perl

Hello, I have a Perl script that reads in an Excel spread sheet and formats the values into a text file. I am having trouble with one column that can have numbers or letters. Excel left justifies the values that start with a letter and right justifies the values that contain only a... (2 Replies)
Discussion started by: jyoung
2 Replies

8. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

9. Shell Programming and Scripting

perl search and replace pairs

Hello all im facing some kind of problem i have this string : functionA() $" "$ functionB("arg1") $" = "$ i will like to replace all the pairs of opening and closing "$" to be something like that functionA() <#" "#> functionB("arg1") <#" = "#> i cant of course do is with simple ... (1 Reply)
Discussion started by: umen
1 Replies

10. UNIX for Dummies Questions & Answers

Global search ok...but replace?

Gurus, I have in my /tmp directory 26 files "filea", "fileb"..."filez". Each file contains the name of a database 'dwora' at many, many places within each file. My boss decided to change the name of the db so I need to do (what i'd call) a global search&replace of that string in all my... (0 Replies)
Discussion started by: alan
0 Replies
Login or Register to Ask a Question