![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 07: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 01:22 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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; |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
You can't use sed?
Code:
sed 's/" "/0/g' file > newfile |
|
#3
|
|||
|
|||
|
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
|
|||
|
|||
|
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. |
|||
| Google The UNIX and Linux Forums |