-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcut.sh
More file actions
executable file
·67 lines (57 loc) · 1.16 KB
/
cut.sh
File metadata and controls
executable file
·67 lines (57 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env bash
usage ()
{
echo "Usage: `basename $0` [ -c N-M ] file"
}
while getopts :hc:d:f: arg
do
case $arg in
h) usage ; exit 0 ;;
c) NUMBERS=$OPTARG ; TYPE="CHAR" ;; # characters
d) DELIMETR=$OPTARG ;; # delimetr
f) FIELDS=$OPTARG ; TYPE="FIELD" ;; # fields numbers
esac
done
shift $(($OPTIND - 1))
DELIMETR=${DELIMETR:=" "}
characters ()
{
local IFS=","
for i in $1
do
[[ ${i} =~ "-" ]] && echo -n ${line:${i%%-*}-1:${i##*-}} || echo -n ${line:${i}-1:1}
done
echo
}
fields ()
{
local DELIM=$1
local FIELDS=$2
local IFS=","
# length_fields=${#FIELDS[@]}
# echo $length_fields
# field_count=0
for field in $FIELDS
do
# field_count=$(( field_count+1 ))
local IFS=$DELIM
count=0
for col in $line
do
count=$(( count+1 ))
[[ $count -eq $field ]] && echo -n $col && echo -n "$DELIM"
# [[ $count -eq $field ]] && echo -n $col && [[ $field_count -ne $length_fields ]] && echo -n "$DELIM"
done
done
echo
}
while read line
do
if [[ $TYPE = "CHAR" ]]
then
characters $NUMBERS
elif [[ $TYPE = "FIELD" ]]
then
fields $DELIMETR $FIELDS
fi
done <$1