Database Reference
In-Depth Information
$
seq 5 | header -a count | body wc -l
count
5
Note that the header is not used, and is also printed again in the output.
The second command-line tool,
header
, allows us, as the name implies, to manipuā
late the header of a CSV file. The complete source code is as follows:
#!/usr/bin/env bash
get_header
()
{
for
i in
$(
seq
$NUMROWS
)
;
do
IFS
=
read
-r LINE
OLDHEADER
=
"${OLDHEADER}${LINE}\n"
done
}
print_header
()
{
echo
-ne
"$1"
}
print_body
()
{
cat
}
OLDHEADER
=
NUMROWS
=
1
while
getopts
"dn:ha:r:e:"
OPTION
do
case
$OPTION
in
n
)
NUMROWS
=
$OPTARG
;;
a
)
print_header
"$OPTARG\n"
print_body
exit
1
;;
d
)
get_header
print_body
exit
1
;;
r
)
get_header
print_header
"$OPTARG\n"
print_body
exit
1
;;
e
)
get_header
print_header
"$(echo -ne $OLDHEADER | eval $OPTARG)\n"