Mercurial > repos > bgruening > text_processing
view replace_text_in_line.xml @ 27:08cdbfffce67 draft
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/text_processing commit 2dc2df988bd2dde9f8d7f629b594186dbd4fdc2b
author | bgruening |
---|---|
date | Fri, 07 Mar 2025 20:43:41 +0000 |
parents | f22a309187a3 |
children |
line wrap: on
line source
<tool id="tp_replace_in_line" name="Replace Text" version="@TOOL_VERSION@+galaxy@VERSION_SUFFIX@" profile="@PROFILE@"> <description>in entire line</description> <macros> <import>macros.xml</import> </macros> <expand macro="creator"/> <requirements> <requirement type="package" version="4.8">sed</requirement> </requirements> <version_command>sed --version | head -n 1</version_command> <command> <![CDATA[ sed -r --sandbox #for $replacement in $replacements: -e '$replacement.sed_options' -e 's/$replacement.find_pattern/$replacement.replace_pattern/g' #end for '$infile' > '$outfile' ]]> </command> <inputs> <param format="txt" name="infile" type="data" label="File to process" /> <repeat name="replacements" title="Replacement" min="1"> <param name="find_pattern" type="text" size="20" label="Find pattern" help="Use simple text, or a valid regular expression (without backslashes // ) " > <sanitizer> <valid initial="string.printable"> <remove value="'"/> <remove value="/"/> </valid> <mapping initial="none"> <add source="'" target="'"'"'" /> <add source="/" target="\/"/> </mapping> </sanitizer> </param> <param name="replace_pattern" type="text" size="20" label="Replace with:" help="Use simple text, or & (ampersand) and \\1 \\2 \\3 to refer to matched text. See examples below." > <sanitizer> <valid initial="string.printable"> <remove value="'"/> <remove value="/"/> </valid> <mapping initial="none"> <add source="'" target="'"'"'" /> <add source="/" target="\/"/> </mapping> </sanitizer> </param> <param name="sed_options" type="text" size="20" optional="true" label="Additional sed commands before replacement" help="Provide additional sed commands before the replacement (e.g., ':a;N;$!ba;')." > <sanitizer> <valid initial="string.printable"> <remove value="'"/> <!-- Removes single quotes --> <remove value="/"/> <!-- Removes slashes --> </valid> <mapping initial="none"> <add source="'" target="'"'"'" /> <!-- Escapes single quotes --> <add source="/" target="\/"/> <!-- Escapes slashes --> </mapping> </sanitizer> </param> </repeat> </inputs> <outputs> <data name="outfile" format_source="infile" metadata_source="infile"/> </outputs> <tests> <test> <param name="infile" value="replace_text_in_line1.txt" /> <repeat name="replacements"> <param name="find_pattern" value="CTC." /> <param name="replace_pattern" value="FOOBAR" /> <param name="sed_options" value="" /> </repeat> <output name="outfile" file="replace_text_in_line_results1.txt" /> </test> <test> <param name="infile" value="replace_text_in_line1.txt" /> <repeat name="replacements"> <param name="find_pattern" value="CTC." /> <param name="replace_pattern" value="FOOBAR" /> <param name="sed_options" value="" /> </repeat> <repeat name="replacements"> <param name="find_pattern" value="chr" /> <param name="replace_pattern" value="domain" /> <param name="sed_options" value="" /> </repeat> <output name="outfile" file="replace_text_in_line_results2.txt" /> </test> <test> <param name="infile" value="replace_text_in_line1.txt" /> <repeat name="replacements"> <param name="find_pattern" value="\n" /> <param name="replace_pattern" value="" /> <param name="sed_options" value=":a;N;$!ba" /> </repeat> <output name="outfile" file="replace_text_in_line_results3.txt" /> </test> </tests> <help> <![CDATA[ **What it does** This tool performs find & replace operation on a specified file. .. class:: infomark The **pattern to find** uses the **extended regular** expression syntax (same as running 'sed -r'). .. class:: infomark **TIP:** If you need more complex patterns, use the *sed* tool. ----- **Examples of Find Patterns** - **HELLO** The word 'HELLO' (case sensitive). - **AG.T** The letters A,G followed by any single character, followed by the letter T. - **A{4,}** Four or more consecutive A's. - **chr2[012]\\t** The words 'chr20' or 'chr21' or 'chr22' followed by a tab character. - **hsa-mir-([^ ]+)** The text 'hsa-mir-' followed by one-or-more non-space characters. When using parenthesis, the matched content of the parenthesis can be accessed with **\1** in the **replace** pattern. **Examples of Replace Patterns** - **WORLD** The word 'WORLD' will be placed whereever the find pattern was found. - **FOO-&-BAR** Each time the find pattern is found, it will be surrounded with 'FOO-' at the beginning and '-BAR' at the end. **&** (ampersand) represents the matched find pattern. - **\\1** The text which matched the first parenthesis in the Find Pattern. ----- **Example 1** **Find Pattern:** HELLO **Replace Pattern:** WORLD Every time the word HELLO is found, it will be replaced with the word WORLD. ----- **Example 2** **Find Pattern:** ^(.{4}) **Replace Pattern:** &\\t Find the first four characters in each line, and replace them with the same text, followed by a tab character. In practice - this will split the first line into two columns. ----- **Extended Regular Expression Syntax** The select tool searches the data for lines containing or not containing a match to the given pattern. A Regular Expression is a pattern descibing a certain amount of text. - **( ) { } [ ] . * ? + \ ^ $** are all special characters. **\\** can be used to "escape" a special character, allowing that special character to be searched for. - **^** matches the beginning of a string(but not an internal line). - **(** .. **)** groups a particular pattern. - **{** n or n, or n,m **}** specifies an expected number of repetitions of the preceding pattern. - **{n}** The preceding item is matched exactly n times. - **{n,}** The preceding item ismatched n or more times. - **{n,m}** The preceding item is matched at least n times but not more than m times. - **[** ... **]** creates a character class. Within the brackets, single characters can be placed. A dash (-) may be used to indicate a range such as **a-z**. - **.** Matches any single character except a newline. - ***** The preceding item will be matched zero or more times. - **?** The preceding item is optional and matched at most once. - **+** The preceding item will be matched one or more times. - **^** has two meaning: - matches the beginning of a line or string. - indicates negation in a character class. For example, [^...] matches every character except the ones inside brackets. - **$** matches the end of a line or string. - **\|** Separates alternate possibilities. **Note**: SED uses extended regular expression syntax, not Perl syntax. **\\d**, **\\w**, **\\s** etc. are **not** supported. However, you can use SED FAQ to perform commands using special characters. More complex options can look like `sed -e '$!N;s/foo/bar/;'`. Here, `$!N;` is an optional part which you only need to set in very special cases. The `foo` part is the search string, and the `bar` part is the replacement string. Please read the SED FAQ here: https://www.pement.org/sed/sedfaq3.html#s3.2 ]]> </help> <expand macro="citations" /> </tool>