Mercurial > repos > gordon > unix_tools
comparison awk.xml @ 0:631dfde45073 draft default tip
First tool-shed public version
| author | gordon |
|---|---|
| date | Tue, 09 Oct 2012 18:48:06 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:631dfde45073 |
|---|---|
| 1 <tool id="cshl_awk_tool1" name="Awk" version="0.1.1"> | |
| 2 <description></description> | |
| 3 <command> | |
| 4 gawk --sandbox -v FS=\$'\t' -v OFS=\$'\t' --re-interval -f '$awk_script' '$input1' > '$output' | |
| 5 </command> | |
| 6 <inputs> | |
| 7 <param format="txt" name="input1" type="data" label="File to process" /> | |
| 8 | |
| 9 <param name="url_paste" type="text" area="true" size="5x35" label="AWK Program" help=""> | |
| 10 <sanitizer> | |
| 11 <valid initial="string.printable"> | |
| 12 <remove value="'"/> | |
| 13 </valid> | |
| 14 </sanitizer> | |
| 15 </param> | |
| 16 | |
| 17 </inputs> | |
| 18 <outputs> | |
| 19 <data format="input" name="output" metadata_source="input1" | |
| 20 /> | |
| 21 </outputs> | |
| 22 <configfiles> | |
| 23 <configfile name="awk_script"> | |
| 24 $url_paste | |
| 25 </configfile> | |
| 26 </configfiles> | |
| 27 <help> | |
| 28 | |
| 29 **What it does** | |
| 30 | |
| 31 This tool runs the unix **awk** command on the selected data file. | |
| 32 | |
| 33 .. class:: infomark | |
| 34 | |
| 35 **TIP:** This tool uses the **extended regular** expression syntax (not the perl syntax). | |
| 36 | |
| 37 | |
| 38 **Further reading** | |
| 39 | |
| 40 - Awk by Example (http://www.ibm.com/developerworks/linux/library/l-awk1.html) | |
| 41 - Long AWK tutorial (http://www.grymoire.com/Unix/Awk.html) | |
| 42 - Learn AWK in 1 hour (http://www.selectorweb.com/awk.html) | |
| 43 - awk cheat-sheet (http://cbi.med.harvard.edu/people/peshkin/sb302/awk_cheatsheets.pdf) | |
| 44 - Collection of useful awk one-liners (http://student.northpark.edu/pemente/awk/awk1line.txt) | |
| 45 | |
| 46 ----- | |
| 47 | |
| 48 **AWK programs** | |
| 49 | |
| 50 Most AWK programs consist of **patterns** (i.e. rules that match lines of text) and **actions** (i.e. commands to execute when a pattern matches a line). | |
| 51 | |
| 52 The basic form of AWK program is:: | |
| 53 | |
| 54 pattern { action 1; action 2; action 3; } | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 **Pattern Examples** | |
| 61 | |
| 62 - **$2 == "chr3"** will match lines whose second column is the string 'chr3' | |
| 63 - **$5-$4>23** will match lines that after subtracting the value of the fourth column from the value of the fifth column, gives value alrger than 23. | |
| 64 - **/AG..AG/** will match lines that contain the regular expression **AG..AG** (meaning the characeters AG followed by any two characeters followed by AG). (This is the way to specify regular expressions on the entire line, similar to GREP.) | |
| 65 - **$7 ~ /A{4}U/** will match lines whose seventh column contains 4 consecutive A's followed by a U. (This is the way to specify regular expressions on a specific field.) | |
| 66 - **10000 < $4 && $4 < 20000** will match lines whose fourth column value is larger than 10,000 but smaller than 20,000 | |
| 67 - If no pattern is specified, all lines match (meaning the **action** part will be executed on all lines). | |
| 68 | |
| 69 | |
| 70 | |
| 71 **Action Examples** | |
| 72 | |
| 73 - **{ print }** or **{ print $0 }** will print the entire input line (the line that matched in **pattern**). **$0** is a special marker meaning 'the entire line'. | |
| 74 - **{ print $1, $4, $5 }** will print only the first, fourth and fifth fields of the input line. | |
| 75 - **{ print $4, $5-$4 }** will print the fourth column and the difference between the fifth and fourth column. (If the fourth column was start-position in the input file, and the fifth column was end-position - the output file will contain the start-position, and the length). | |
| 76 - If no action part is specified (not even the curly brackets) - the default action is to print the entire line. | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 **AWK's Regular Expression Syntax** | |
| 87 | |
| 88 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. | |
| 89 | |
| 90 - **( ) { } [ ] . * ? + \ ^ $** are all special characters. **\\** can be used to "escape" a special character, allowing that special character to be searched for. | |
| 91 - **^** matches the beginning of a string(but not an internal line). | |
| 92 - **(** .. **)** groups a particular pattern. | |
| 93 - **{** n or n, or n,m **}** specifies an expected number of repetitions of the preceding pattern. | |
| 94 | |
| 95 - **{n}** The preceding item is matched exactly n times. | |
| 96 - **{n,}** The preceding item ismatched n or more times. | |
| 97 - **{n,m}** The preceding item is matched at least n times but not more than m times. | |
| 98 | |
| 99 - **[** ... **]** 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**. | |
| 100 - **.** Matches any single character except a newline. | |
| 101 - ***** The preceding item will be matched zero or more times. | |
| 102 - **?** The preceding item is optional and matched at most once. | |
| 103 - **+** The preceding item will be matched one or more times. | |
| 104 - **^** has two meaning: | |
| 105 - matches the beginning of a line or string. | |
| 106 - indicates negation in a character class. For example, [^...] matches every character except the ones inside brackets. | |
| 107 - **$** matches the end of a line or string. | |
| 108 - **\|** Separates alternate possibilities. | |
| 109 | |
| 110 | |
| 111 **Note**: AWK uses extended regular expression syntax, not Perl syntax. **\\d**, **\\w**, **\\s** etc. are **not** supported. | |
| 112 | |
| 113 </help> | |
| 114 </tool> |
