]> Pileus Git - ~andy/sfvlug/blob - awk/src/awk-4-vcutfile.sh
Add sed and awk presentation
[~andy/sfvlug] / awk / src / awk-4-vcutfile.sh
1 #!/bin/bash
2
3 # cut file
4 bnd="${CONTENT_TYPE/*boundary\=/}"
5
6 awk -v "want=$1" -v "bnd=$bnd" '
7         BEGIN { RS="\r\n" }
8
9         # reset based on boundaries
10         $0 == "--"bnd""     { st=1; next; }
11         $0 == "--"bnd"--"   { st=0; next; }
12         $0 == "--"bnd"--\r" { st=0; next; }
13
14         # search for wanted file
15         st == 1 && $0 ~  "^Content-Disposition:.* name=\""want"\"" { st=2; next; }
16         st == 1 && $0 == "" { st=9; next; }
17
18         # wait for newline, then start printing
19         st == 2 && $0 == "" { st=3; next; }
20         st == 3             { print $0    }
21         # head
22 ' | head -c $((128*1024)) # Limit size to 128K