Hello.
Another approach (it respects your specifications) :
Code:
#!/bin/bash
if (($# != 1)) || [[ ! -f $1 ]]; then
printf 'E : one file has to be passed as argument.\n' >&2
exit 1
fi
printf 'scsi3, vendor, product_name, serial_no, protocol_used, transport, quirks\n'
while read -r; do
case "$REPLY" in
'')
printf '\n'
i=0
read -r
;;
*:)
read -r
esac
if ((i++)); then
printf ', '
fi
printf %s "${REPLY#*:[[:blank:]]}"
done < "$1"
# if the file is not newline terminated
if ((i)); then
printf '\n'
fi
Though, it's faster with
awk(1) (like 10ms less) and you don't have to check whether the file is newline terminated or not.
Here is the code :
Code:
#!/usr/bin/awk -f
BEGIN \
{
FS = ": "
print "scsi3, vendor, product_name, serial_no, protocol_used, transport, quirks"
}
{
while ($0 ~ /^.*:$/) {
getline nl
printf(", %s", nl)
next
}
while (! NF) {
print
i = 0
next
}
if (i++)
printf(", ")
printf("%s", $2)
}
END \
{
if (i)
printf("\n")
}
You might want the « quick awk version », so here it is :
Code:
awk -F ': ' 'BEGIN {print "scsi3, vendor, product_name, serial_no, protocol_used, transport, quirks"} /^.*:$/ {getline nl; printf(", %s", nl); next} ! NF {print; i = 0; next} i++ {printf(", ")} {printf("%s", $2)} END {if (i) printf("\n")}' <file>