annotate tools/unix_tools/remove_ending.sh @ 0:9071e359b9a3

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:37:19 -0500
parents
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 # Version 0.1 , 15aug08
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 # Written by Assaf Gordon (gordon@cshl.edu)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 #
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 LINES="$1"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 INFILE="$2"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 OUTFILE="$3"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 if [ "$LINES" == "" ]; then
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 cat >&2 <<EOF
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 Remove Ending Lines
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 Usage: $0 LINES [INFILE] [OUTFILE]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 LINES - number of lines to remove from the end of the file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 [INFILE] - input file (if not specified - defaults to STDIN)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 [OUTFILE]- output file (if not specified - defaults to STDOUT)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 Input Example:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 #Chr Start End
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 chr1 10 15
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 chr1 40 20
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 chr1 21 14
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 total 3 chromosomes
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 Removing 1 line (the last line) produces:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 #Chr Start End
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 chr1 10 15
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 chr1 20 40
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 chr 14 21
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 Usage Example:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 \$ $0 1 < my_input_file.txt > my_output_file.txt
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 EOF
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 exit 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 fi
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 #Validate line argument - remove non-digits characters
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 LINES=${LINES//[^[:digit:]]/}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 #Make sure the line strings isn't empty
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 #(after the regex above, they will either contains digits or be empty)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 if [ -z "$LINES" ]; then
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 echo "Error: bad line value (must be numeric)" >&2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 exit 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 fi
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 # Use default (stdin/out) values if infile / outfile not specified
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 [ -z "$INFILE" ] && INFILE="/dev/stdin"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 [ -z "$OUTFILE" ] && OUTFILE="/dev/stdout"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 #Make sure the input file (if specified) exists.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 if [ ! -r "$INFILE" ]; then
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 echo "Error: input file ($INFILE) not found!" >&2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 exit 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 fi
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 # The "gunzip -f" trick allows
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 # piping a file (gzip or plain text, real file name or "/dev/stdin") to sed
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 gunzip -f <"$INFILE" | sed -n -e :a -e "1,${LINES}!{P;N;D;};N;ba" > "$OUTFILE"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69