Need to get a comma delimited list filenames in an email. Here is some input examples with 1 having lots of special characters:
Content-Disposition: attachment;
filename="How-To_21_Monitor_Mode_Deployment_Guide.pdf"; size=3886046;
creation-date="Fri, 27 Oct 2017 16:23:20 GMT";
modification-date="Fri, 27 Oct 2017 16:24:30 GMT"
Content-Disposition: attachment; filename="How-To_24_Low_Impact_Mode.pdf";
size=6714113; creation-date="Fri, 27 Oct 2017 16:23:20 GMT";
modification-date="Fri, 27 Oct 2017 16:24:31 GMT"
Content-Disposition: attachment;
filename="SBTGxYVWPE1wI9SAjl5b2PUfF1LCjbU3aChsoch5eXuI4GrIP9bRhfiaOuwL1U
;.,~!@#$%....txt"; size=3966; creation-date="Fri, 27 Oct 2017 16:23:20 GMT";
modification-date="Fri, 27 Oct 2017 16:23:20 GMT"
This works, but concerned if it went more than 3 lines and it is not efficient:
grep --no-group-separator --line-buffered -A 2 '^Content-Disposition: ' | sed -e '/\;$/!{N;s/\n//}' -n -e 's/.*filename\=//p' | sed -e 's/ size\=.*//' | sed 's/\;$//' | sed ':a;N;$!ba;s/\n/,/g'
Tried to get it working with a single sed line:
sed -n '/^Content-Disposition: /,/\"\; size\=/{/\;$/!{x;N;s/\n//g}};s/.*filename\=//p;s/ size\=.*//;s/\;$//;:a;N;$!ba;s/\n/,/g;
Would be appreciated for a single command preferably with sed.
sed
. You probably won't reconstruct it withawk
, either. You need a full MIME parser to deal with all cases.