annotate tools/unix_tools/grep_wrapper_old.sh @ 1:cdcb0ce84a1b

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:15 -0500
parents 9071e359b9a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/bin/sh
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 ##
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 ## Galaxy wrapper for GREP command.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 ##
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 ##
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 ## command line arguments:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 ## input_file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 ## output_file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 ## regex
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 ## COLOR or NOCOLOR
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 ## [other parameters passed on to grep]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 INPUT="$1"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 OUTPUT="$2"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 REGEX="$3"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 COLOR="$4"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 shift 4
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 if [ -z "$COLOR" ]; then
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 echo usage: $0 INPUTFILE OUTPUTFILE REGEX COLOR\|NOCOLOR [other grep patameters] >&2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 exit 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 fi
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 if [ ! -r "$INPUT" ]; then
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 echo "error: input file ($INPUT) not found!" >&2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 exit 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 fi
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 # Messages printed to STDOUT will be displayed in the "INFO" field in the galaxy dataset.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 # This way the user can tell what was the command
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 echo "grep" "$@" "$REGEX"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 if [ "$COLOR" == "COLOR" ]; then
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 #
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 # What the heck is going on here???
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 # 1. "GREP_COLORS" is an environment variable, telling GREP which ANSI colors to use.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 # 2. "--colors=always" tells grep to actually use colors (according to the GREP_COLORS variable)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 # 3. first sed command translates the ANSI color to a <FONT> tag with blue color (and a <B> tag, too)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 # 4. second sed command translates the no-color ANSI command to a </FONT> tag (and a </B> tag, too)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 # 5. htmlize_pre scripts takes a text input and wraps it in <HTML><BODY><PRE> tags, making it a fixed-font HTML file.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 GREP_COLORS="ms=31" grep --color=always -P "$@" -- "$REGEX" "$INPUT" | \
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 grep -v "^\[36m\[K--\[m\[K$" | \
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 sed -r 's/\[[0123456789;]+m\[K?/<font color="blue"><b>/g' | \
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 sed -r 's/\[m\[K?/<\/b><\/font>/g' | \
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 htmlize_pre.sh > "$OUTPUT"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 if (( $? )); then exit; fi
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 elif [ "$COLOR" == "NOCOLOR" ]; then
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 grep -P "$@" -- "$REGEX" "$INPUT" | grep -v "^--$" > "$OUTPUT"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 if (( $? )); then exit; fi
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 else
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 echo Error: third parameter must be "COLOR" or "NOCOLOR" >&2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 exit 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 fi
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 exit 0