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