|
| 1 | +#!/usr/bin/ruby |
| 2 | +# ************************************************************************** |
| 3 | +# ec (say "extract column") |
| 4 | +# Written by Christian Wolf |
| 5 | +# |
| 6 | +# This program is free software; you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU General Public License as published by |
| 8 | +# the Free Software Foundation; either version 2 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# This program is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU General Public License |
| 17 | +# along with this program; if not, write to the |
| 18 | +# Free Software Foundation, Inc., |
| 19 | +# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 20 | +# |
| 21 | +# ************************************************************************** |
| 22 | +# |
| 23 | +# Changelog: |
| 24 | +# 2.1 21.06.2009: Included range processing, i.e. columns 1-16 |
| 25 | +# 2.0 13.10.2005: Port to the ruby programming language |
| 26 | +# 1.0 03.07.2005: changed syntax: allow multiple rows to be selected |
| 27 | +# 0.5 xx.xx.2002: start of project |
| 28 | +# ************************************************************************** |
| 29 | + |
| 30 | +$Version="2.1 21.06.2009" |
| 31 | + |
| 32 | +$help=<<END_MESSAGE |
| 33 | +ec is a tool which is able to extract columns from a text file. Just pipe |
| 34 | +your data through the ec command and specify the column indices as arguments |
| 35 | +(columns begin with 1), e.g.: |
| 36 | +
|
| 37 | +cat data.txt | ec 2 3 |
| 38 | +
|
| 39 | +Ranges can be specified using the dash operator, i.e. 4-7. Do not put |
| 40 | +whitespace before and after the dash. The starting and endpoint need to |
| 41 | +be specified, i.e. -3 or 7- are not allowed. |
| 42 | + |
| 43 | +ec is also usefull if the columns in the original file are not separated by |
| 44 | +whitespace but by another character. The character separater used by ec |
| 45 | +can configured by adding it on the command line preceeded by a dash. |
| 46 | +The output character separater is always whitespace. If, for example, |
| 47 | +you have a file whose columns are seperated by a semicolon and you want to |
| 48 | +plot columns 5 and 3, use the command: |
| 49 | +
|
| 50 | +cat data.txt | ec -; 5 3 |
| 51 | +
|
| 52 | +License: GPL; this is free software. There is absolutely NO warranty!! |
| 53 | +END_MESSAGE |
| 54 | + |
| 55 | +# ---- The usage message |
| 56 | +def usage |
| 57 | + printf "usage: %s [ -<separator> ] { <column-number> }\n",$0 |
| 58 | + printf "\n%s\n",$help |
| 59 | + exit 1 |
| 60 | +end |
| 61 | + |
| 62 | +# ---- the main program |
| 63 | +def main |
| 64 | + |
| 65 | + # ---- Check Arguments |
| 66 | + usage if ARGV.length < 1 |
| 67 | + usage if ARGV[0]=="-h" |
| 68 | + if ARGV[0]=="-v" |
| 69 | + printf "ec version %s\n",$Version |
| 70 | + printf "Written by Christian Wolf\n" |
| 71 | + printf "License: GPL; this is free software. " |
| 72 | + puts "There is absolutely NO warranty!!" |
| 73 | + exit 0 |
| 74 | + end |
| 75 | + |
| 76 | + # ---- Create the delimiter |
| 77 | + delimiter=" " |
| 78 | + delimiter=ARGV[0][1,ARGV[0].length-1] if ARGV[0][0,1]=="-" |
| 79 | + |
| 80 | + # ---- Create the array with separator indices |
| 81 | + indices=[] |
| 82 | + ARGV.each do |x| |
| 83 | + if x[0,1]!="-" |
| 84 | + pos=x.index("-") |
| 85 | + if pos==nil |
| 86 | + indices.push(x.to_i-1) |
| 87 | + else |
| 88 | + ibeg=x[0,pos].to_i |
| 89 | + iend=x[pos+1,x.length-pos].to_i |
| 90 | + (ibeg..iend).each { |y| indices.push(y-1) } |
| 91 | + end |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + # ---- Traverse the lines |
| 96 | + while line = $stdin.gets |
| 97 | + line["\n"]="" |
| 98 | + arr=line.split(delimiter) |
| 99 | + indices.each { |i| printf "%s ",arr[i] } |
| 100 | + puts |
| 101 | + end |
| 102 | +end |
| 103 | + |
| 104 | +main |
0 commit comments