04-07-2008
Did you try the code I posted? Does it do roughly what you asked for? If not, can you tell what more you need?
Canonical representation basically means you come up with a "standard" which all other representations can be converted into. (I wrote that before looking at the first sentence in the Wikipedia article -- honest.)
What I suggested was to simply use a canonicalization which says they should be in alphabetic order, so you can compare them head to head; in other words, find a way to mask off anything which introduces an artificial difference when you know two forms to be identical. Now if you rewrite them in this way, then simple string equality can be used to test for equivalence.
The code I posted already does this, but doesn't handle lines with more than three fields. Just add a print in the ++a[stuff] part if you want to keep them and add a frequency count at the end. You need a way to separate the frequency count from the raw data, though, so I'd just keep them in separate files, and not add that print statement.
PS. The wikipedia link is slightly broken. You want
Canonicalization - Wikipedia, the free encyclopedia
10 More Discussions You Might Find Interesting
1. Shell Programming and Scripting
Hi,
I have a file that looks something like this:
...
0,6,256,87,0,0,0,1187443420
0,6,438,37,0,0,0,1187443380
0,2,0,0,0,10,0,1197140320
0,3,0,0,0,10,0,1197140875
0,2,0,0,0,23,0,1197140332
0,3,0,0,0,23,0,1197140437
0,2,0,0,0,17,0,1197140447
0,3,0,0,0,17,0,1197140543... (8 Replies)
Discussion started by: csejl
8 Replies
2. UNIX for Dummies Questions & Answers
hi,
i want to create a script that will search and replace the values inside a particular file. i have 5 files that i need to change some values inside and i don't want to use vi to edit these files. All the inputted values on the script below will be passed into the files.
cho ""
echo... (3 Replies)
Discussion started by: tungaw2004
3 Replies
3. UNIX for Dummies Questions & Answers
I am using perl to perform a search and replace. It works at the command line, but not in the csh shell script
perl -pi -e 's@/Pattern@@g' $path/$file
I used the @ as my delimiter because the pattern contains "/" (3 Replies)
Discussion started by: NobluesFDT
3 Replies
4. UNIX for Dummies Questions & Answers
Hi,
I am trying to write a shell script designed to take input line by line by line from a file with a word on each line for editing with sed. Example file:
1.ejverything
2.bllown
3.maikling
4.manegement
5.existjing
6.systems
My design currently takes input from the user, and... (2 Replies)
Discussion started by: mkfitzwilliams
2 Replies
5. Shell Programming and Scripting
I have a text file for which i need a script which does some fancy search and replace.
Basically i want to loop through each line, if i find an occurance of certain string format then i want to carry on search on replace another line, once i replaced this line i will contine to search for the... (7 Replies)
Discussion started by: kelseyh
7 Replies
6. Shell Programming and Scripting
Please Help to Check script Search and Replace
Ex. Search 0001 and Replete un_0001
---script
Code:
nawk -F\" 'NR==FNR{a;next}$2 in a{sub($2,"un_"$2)}1' input.txt file*.txt > resoult.txt
script is work to one result but
if i have file1.txt, file2.txt, file3.txt i want to Replace... (5 Replies)
Discussion started by: kittiwas
5 Replies
7. Shell Programming and Scripting
Ok,
what about:
array set simulation_frames { ... }
foreach { frames } {
writepdb pdb_$frames.pdb
}Now, my question is simply, what strategy could I use to import my numbers into the array { ... }
I could manually copy them, and that would work, but is there another way? (2 Replies)
Discussion started by: chrisjorg
2 Replies
8. Shell Programming and Scripting
Hi All,
I am trying to write a script which will find a particular text in certain group of files under a directory and if found correctly it will replace them with a new text in all the files.
Could any one let me know how do i find the text in many files under a directory.
Thanks (3 Replies)
Discussion started by: chetansingh23
3 Replies
9. Shell Programming and Scripting
Hi,
Below is the script which will find a particular text and replace with another one in a group of files under a directory /test
#!/bin/bash
old=$1 --- first input old text
new=$2--- input new text
cd /test --- folder into which files need to be checked
for y in `ls *`;
do sed... (2 Replies)
Discussion started by: chetansingh23
2 Replies
10. UNIX for Dummies Questions & Answers
Hi,
I have an input file with below data and rules file to apply search and replace by each field in the input based on exact value or pattern.
Could you please help me with unix script to read input file and rules file and then create the output and reject files based on the rules file.
Input... (13 Replies)
Discussion started by: chandrath
13 Replies
LEARN ABOUT PHP
str_replace
STR_REPLACE(3) 1 STR_REPLACE(3)
str_replace - Replace all occurrences of the search string with the replacement string
SYNOPSIS
mixed str_replace (mixed $search, mixed $replace, mixed $subject, [int &$count])
DESCRIPTION
This function returns a string or an array with all occurrences of $search in $subject replaced with the given $replace value.
If you don't need fancy replacing rules (like regular expressions), you should always use this function instead of preg_replace(3).
PARAMETERS
If $search and $replace are arrays, then str_replace(3) takes a value from each array and uses them to search and replace on $subject. If
$replace has fewer values than $search, then an empty string is used for the rest of replacement values. If $search is an array and
$replace is a string, then this replacement string is used for every value of $search. The converse would not make sense, though.
If $search or $replace are arrays, their elements are processed first to last.
o $search
- The value being searched for, otherwise known as the needle. An array may be used to designate multiple needles.
o $replace
- The replacement value that replaces found $search values. An array may be used to designate multiple replacements.
o $subject
- The string or array being searched and replaced on, otherwise known as the haystack. If $subject is an array, then the search
and replace is performed with every entry of $subject, and the return value is an array as well.
o $count
- If passed, this will be set to the number of replacements performed.
RETURN VALUES
This function returns a string or an array with the replaced values.
EXAMPLES
Example #1
Basic str_replace(3) examples
<?php
// Provides: <body text='black'>
$bodytag = str_replace("%body%", "black", "<body text='%body%'>");
// Provides: Hll Wrld f PHP
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");
// Provides: You should eat pizza, beer, and ice cream every day
$phrase = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy = array("pizza", "beer", "ice cream");
$newphrase = str_replace($healthy, $yummy, $phrase);
// Provides: 2
$str = str_replace("ll", "", "good golly miss molly!", $count);
echo $count;
?>
Example #2
Examples of potential str_replace(3) gotchas
<?php
// Order of replacement
$str = "Line 1
Line 2
Line 3
Line 4
";
$order = array("
", "
", "
");
$replace = '<br />';
// Processes
's first so they aren't converted twice.
$newstr = str_replace($order, $replace, $str);
// Outputs F because A is replaced with B, then B is replaced with C, and so on...
// Finally E is replaced with F, because of left to right replacements.
$search = array('A', 'B', 'C', 'D', 'E');
$replace = array('B', 'C', 'D', 'E', 'F');
$subject = 'A';
echo str_replace($search, $replace, $subject);
// Outputs: apearpearle pear
// For the same reason mentioned above
$letters = array('a', 'p');
$fruit = array('apple', 'pear');
$text = 'a p';
$output = str_replace($letters, $fruit, $text);
echo $output;
?>
NOTES
Note
This function is binary-safe.
Caution
Replacement order gotcha
Because str_replace(3) replaces left to right, it might replace a previously inserted value when doing multiple replacements. See
also the examples in this document.
Note
This function is case-sensitive. Use str_ireplace(3) for case-insensitive replace.
SEE ALSO
str_ireplace(3), substr_replace(3), preg_replace(3), strtr(3).
PHP Documentation Group STR_REPLACE(3)