Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Rafal Bogdanowicz
linuxcommandline
Commits
8157ecd2
Commit
8157ecd2
authored
Jan 25, 2017
by
Toby Hodges
Browse files
added Toby's version of column chooser script
parent
e08e4e0a
Changes
1
Hide whitespace changes
Inline
Side-by-side
linux_intermediate/exercise_files/columnchooser_v2.sh
0 → 100644
View file @
8157ecd2
#! /usr/bin/bash
USAGE
=
"columnchooser_v2.sh FILE COLNAME [COLNAME [...]]"
# AUTHOR: Toby Hodges (toby.hodges@embl.de)
if
[
$#
-lt
2
]
then
echo
"USAGE:
$USAGE
"
exit
1
fi
file
=
$1
shift
cols
=
$@
read
-a
headers
<<<
`
head
-n1
$file
`
numfields
=
${#
headers
[@]
}
declare
-a
outfields
=()
declare
-i
matchnum
=
0
unmatched
=
""
for
col
in
${
cols
[@]
}
do
matched
=
0
for
colnum
in
$(
seq
$numfields
)
do
if
[
${
headers
[
$colnum
-1]
}
=
$col
]
then
outfields[
$matchnum
]=
"
\$
$colnum
"
matchnum
=
$((
$matchnum
+
1
))
matched
=
1
fi
done
if
[
$matched
=
0
]
then
unmatched
=
"
$unmatched
$col
"
fi
done
if
[
$unmatched
!=
""
]
then
echo
"Sorry, no column(s) named
$unmatched
"
1>&2
fi
awkfields
=
$(
IFS
=
,
;
echo
"
${
outfields
[*]
}
"
)
awk
"{ print
$awkfields
}"
$file
exit
0
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment