All the comments on your question are good ones. There is no support for CSV built in to bash, so if you don't want to use a language like Python, Ruby, Erlang or even Perl, you have to roll your own.
Note that while awk
can use commas as a field separator, it also does not properly support CSV with commas embedded in quoted fields. You can hack together a solution with a pattern, as Håkon suggested.
But you don't need to do this in awk; you can do it just as well in bash alone and avoid calling an external tool. How about something like this?
#!/bin/bash
nextfield () {
case "$line" in
\"*)
value="${line%%\",*}\""
line="${line#*\",}"
;;
*)
value="${line%%,*}"
line="${line#*,}"
;;
esac
}
# loop through the file
while read line; do
# get the content
nextfield; vulnid="$value"
nextfield; ruleid="$value"
nextfield; stigid="$value"
nextfield; title="$value"
nextfield; discussion="$value"
nextfield; check="$value"
nextfield; fix="$value"
# format the content
printf "########################################################\n"
printf "# Vulnerability ID: %s\n" "$vulnid"
printf "# Rule ID: %s\n# STIG ID: %s\n#\n" "$ruleid" "$stigid"
printf "# Rule: %s\n" "$title"
printf "#\n# Discussion:\n"
fmt -w68 <<<"$discussion" | sed 's/^/# /'
printf "# Check:\n"
fmt -w68 <<<"$check" | sed 's/^/# /'
printf "# Fix:\n"
fmt -w68 <<<"$fix" | sed 's/^/# /'
printf "########################################################\n"
printf "# Start Check\n\n"
printf "# Start Remediation\n\n"
printf "########################################################\n"
done < STIG.csv
The speed advantage will be tremendous, if you're doing a lot of these.
Note the improved formatting, courtesy of fmt
. This kind of kills the speed benefit of avoiding the call to an external program, but it sure makes your output easier to read. :)