-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfields
More file actions
executable file
·43 lines (35 loc) · 754 Bytes
/
Copy pathfields
File metadata and controls
executable file
·43 lines (35 loc) · 754 Bytes
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
#!/usr/bin/env bash
delimiter=' '
flags=("Fields: a small script that just prints out fields from either a file input or stdin" " -dl []: delimiter character default ' '")
if [ $# == 0 ]; then
echo "Pass the number of the field"
exit 1
fi
init="{print"
file_name=""
is_digit() {
[[ "$1" =~ ^[0-9]+$ ]]
}
i=1
while [ $i -le $# ]; do
flag="${!i}"
if is_digit "$flag"; then
init+=" \$$flag"
if ! [ $i = $# ]; then
init+=", "
fi
elif [ "$flag" = "-dl" ]; then
i=$((i + 1))
delimiter="${!i}"
elif [ "$flag" = "-h" ]; then
for flag in "${flags[@]}"; do
echo "$flag"
done
exit 0
else
file_name="${!i}"
fi
i=$((i + 1))
done
init+="}"
awk "$init" FS="$delimiter" OFS=' ' "$file_name"