Mercurial > repos > bgruening > text_processing
comparison replace_text_in_column.xml @ 3:7068d1548234 draft
Uploaded
author | bgruening |
---|---|
date | Sun, 06 Oct 2013 08:22:36 -0400 |
parents | |
children | 56e80527c482 |
comparison
equal
deleted
inserted
replaced
2:fc862d5bccaf | 3:7068d1548234 |
---|---|
1 <tool id="tp_replace_in_column" name="Replace Text" version="0.1"> | |
2 <description>in a specific column</description> | |
3 <requirements> | |
4 <requirement type="package" version="4.1.0">gnu_awk</requirement> | |
5 </requirements> | |
6 <command interpreter="sh"> | |
7 #adapt to awk's quirks - to pass an acutal backslash - two backslashes are required (just like in a C string) | |
8 REPLACE_PATTERN=\${$replace_pattern//\\/\\\\}; | |
9 awk -v OFS="\t" --re-interval --sandbox "{ \$$column = gensub( /$find_pattern/, \"$replace_pattern\", \"g\", \$$column ) ; print \$0 ; }" "$input" > "$output" | |
10 </command> | |
11 <inputs> | |
12 <param format="tabular" name="input" type="data" label="File to process" /> | |
13 <param name="column" label="in column" type="data_column" data_ref="input" accept_default="true" /> | |
14 | |
15 <param name="find_pattern" type="text" size="20" label="Find pattern" help="Use simple text, or a valid regular expression (without backslashes // ) " > | |
16 <sanitizer> | |
17 <valid initial="string.printable"> | |
18 <remove value="'"/> | |
19 </valid> | |
20 </sanitizer> | |
21 </param> | |
22 | |
23 <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." > | |
24 <sanitizer> | |
25 <valid initial="string.printable"> | |
26 <remove value="'"/> | |
27 </valid> | |
28 </sanitizer> | |
29 </param> | |
30 | |
31 </inputs> | |
32 <tests> | |
33 <test> | |
34 <param name="input" value="replace_text_in_column_in1.txt" ftype="tabular" /> | |
35 <output name="output" file="replace_text_in_column_output1.txt" /> | |
36 <param name="column" value="4" /> | |
37 <param name="url_paste" value=".+_(R.)" /> | |
38 <param name="file_data" value="\1" /> | |
39 </test> | |
40 </tests> | |
41 <outputs> | |
42 <data format="input" name="output" metadata_source="input" /> | |
43 </outputs> | |
44 <help> | |
45 | |
46 **What it does** | |
47 | |
48 This tool performs find & replace operation on a specified column in a given file. | |
49 | |
50 .. class:: infomark | |
51 | |
52 The **pattern to find** uses the **extended regular** expression syntax (same as running 'awk --re-interval'). | |
53 | |
54 .. class:: infomark | |
55 | |
56 **TIP:** If you need more complex patterns, use the *awk* tool. | |
57 | |
58 ----- | |
59 | |
60 | |
61 **Examples of Find Patterns** | |
62 | |
63 - **HELLO** The word 'HELLO' (case sensitive). | |
64 - **AG.T** The letters A,G followed by any single character, followed by the letter T. | |
65 - **A{4,}** Four or more consecutive A's. | |
66 - **chr2[012]\\t** The words 'chr20' or 'chr21' or 'chr22' followed by a tab character. | |
67 - **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. | |
68 | |
69 | |
70 **Examples of Replace Patterns** | |
71 | |
72 - **WORLD** The word 'WORLD' will be placed whereever the find pattern was found. | |
73 - **FOO-&-BAR** Each time the find pattern is found, it will be surrounded with 'FOO-' at the begining and '-BAR' at the end. **&** (ampersand) represents the matched find pattern. | |
74 - **\\1** The text which matched the first parenthesis in the Find Pattern. | |
75 | |
76 | |
77 | |
78 | |
79 ----- | |
80 | |
81 **Example 1** | |
82 | |
83 **Find Pattern:** HELLO | |
84 **Replace Pattern:** WORLD | |
85 | |
86 Every time the word HELLO is found, it will be replaced with the word WORLD. This operation affects only the selected column. | |
87 | |
88 ----- | |
89 | |
90 **Example 2** | |
91 | |
92 **Find Pattern:** ^(.{4}) | |
93 **Replace Pattern:** &\\t | |
94 | |
95 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. This operation affects only the selected column. | |
96 | |
97 | |
98 ----- | |
99 | |
100 **Extened Regular Expression Syntax** | |
101 | |
102 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. | |
103 | |
104 - **( ) { } [ ] . * ? + \ ^ $** are all special characters. **\\** can be used to "escape" a special character, allowing that special character to be searched for. | |
105 - **^** matches the beginning of a string(but not an internal line). | |
106 - **(** .. **)** groups a particular pattern. | |
107 - **{** n or n, or n,m **}** specifies an expected number of repetitions of the preceding pattern. | |
108 | |
109 - **{n}** The preceding item is matched exactly n times. | |
110 - **{n,}** The preceding item ismatched n or more times. | |
111 - **{n,m}** The preceding item is matched at least n times but not more than m times. | |
112 | |
113 - **[** ... **]** 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**. | |
114 - **.** Matches any single character except a newline. | |
115 - ***** The preceding item will be matched zero or more times. | |
116 - **?** The preceding item is optional and matched at most once. | |
117 - **+** The preceding item will be matched one or more times. | |
118 - **^** has two meaning: | |
119 - matches the beginning of a line or string. | |
120 - indicates negation in a character class. For example, [^...] matches every character except the ones inside brackets. | |
121 - **$** matches the end of a line or string. | |
122 - **\|** Separates alternate possibilities. | |
123 | |
124 | |
125 **Note**: AWK uses extended regular expression syntax, not Perl syntax. **\\d**, **\\w**, **\\s** etc. are **not** supported. | |
126 | |
127 </help> | |
128 </tool> |