Building a general solution to most problems of this kind.
Using a general start
, middle
and end
string
bash A solution with only bash:
#!/bin/bash
infile="infile"
start='"'
middle='", "'
end='"'
res="$start" # start the result with str "$start".
while IFS=$'\n' read -r line # for each line in the file.
do res="${res}${line}${middle}" # add the line and the middle str.
done <"$infile" # for file "$infile"
res="${res%"$middle"}${end}" # remove "$middle" on the last line.
printf '%s\n' "${res}" # print result.
awk
And a solution for larger files with awk:
#!/bin/bash
infile="infile"
start='"'
middle='", "'
end='"'
awk -vs="$start" -vm="$middle" -ve="$end" '
BEGIN{printf("%s",s)}
{
if(ll){printf("%s%s",ll,m)}
ll=$0
}
END{printf("%s%s%s",ll,e,"\n")}
' "$infile"