forked from gregorgorjanc/GorjancShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc_dim.sh
62 lines (58 loc) · 1.23 KB
/
func_dim.sh
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
# NROW, NCOL, and DIM
#-------------------------------------------------------------------------------
# What: Count the number of rows and columns in a file
# Who: Gregor Gorjanc
# When:
# * 2014-01 Make them independent
# * 2012-12 Initial version
#-------------------------------------------------------------------------------
dim()
{
## $1 $2 = potentially a delimiter, i.e., -d ,
## $@ = file names
local DELIM FILE NROW NCOL
if [[ $1 == "-d" ]]; then
DELIM=$2
shift 2
else
DELIM=" "
fi
# echo "DELIM is |$DELIM|"
# echo "args are $@"
for FILE in $@; do
NROW=$(wc -l $FILE | awk '{ print $1}')
NCOL=$(head -n 1 $FILE | awk -F "$DELIM" '{ print NF }')
echo "$NROW $NCOL $FILE"
unset NROW NCOL
done
}
export -f dim
nrow()
{
## $@ = file names
local FILE
for FILE in $@; do
wc -l $FILE
done
}
export -f nrow
ncol()
{
## $1 $2 = potentially a delimiter, i.e., -d ,
## $@ = file names
local DELIM FILE NCOL
if [[ $1 == "-d" ]]; then
DELIM=$2
shift 2
else
DELIM=" "
fi
# echo "DELIM is |$DELIM|"
# echo "args are $@"
for FILE in $@; do
NCOL=$(head -n 1 $FILE | awk -F "$DELIM" '{ print NF }')
echo "$NCOL $FILE"
unset NCOL
done
}
export -f ncol