0

I'm trying to match a pattern and then pull the following information to create a file using that variable.

Example (my data may look like either of the 2 examples)

"headers": {
            "content-type": "application/octet-stream;\r\n\tname=\"I may have long file names - With lots of spaces.zip\"",
            "content-transfer-encoding": "base64",
            "content-disposition": "attachment;\r\n\tfilename=\"I may have long file names - With lots of spaces.zip\""

or

"headers": {
            "content-type": "application/octet-stream",
            "content-transfer-encoding": "base64",
            "content-disposition": "attachment; filename=tiny.exe"

Basically I'm trying to create a new file named as the filename. So example 1 has a really long filename with spaces and hypens, example 2 has a short filename. Example 1 is identified by 'tfilename' and example 2 'filename' .

Obviously I don't need the double quotes.

Can anyone assist with this please?

Thanks!

2
  • Is this JSON data? Commented Jan 20, 2015 at 21:50
  • Yes. I'm using JQ to process the JSON. But the problem with this particular section is that the information I'm seeking is deep within the object. Using JQ and Grep, I got it down to this point. So now I'm just trying to grab the filename so that I can (1) store it as a variable & (2) create a new file using that name. Commented Jan 20, 2015 at 21:59

1 Answer 1

1

I would consider using a language that can parse the JSON and manipulate the data. Here's some ruby (and shell)

$ cat data.json 
{
    "headers": {
        "content-type": "application/octet-stream;\r\n\tname=\"I may have long file names - With lots of spaces.zip\"",
        "content-transfer-encoding": "base64",
        "content-disposition": "attachment;\r\n\tfilename=\"I may have long file names - With lots of spaces.zip\""
    }
}

$ filename=$(ruby -rjson -e '
    data = JSON.parse(File.read(ARGV.shift))
    m = data["headers"]["content-disposition"].match(/filename=(.+)$/m)
    filename = m[1].gsub(/^"|"$/,"")
    puts filename
' data.json)

$ echo "$filename"
I may have long file names - With lots of spaces.zip
3
  • Thats really nice, but I don't really want a ruby implementation just for this one aspect. I'm already using jq to parse the data. Surely there is a simple awk or sed command to pull the info. I ended up getting this, but it's a bit janky: cat file | jq -r '.parts[1].headers."content-disposition"' | sed -n -e '2p' | sed s/'filename='/''/ | sed -e s/'"'/''/ | sed -e s/'"'/''/ Commented Jan 20, 2015 at 22:54
  • untested, but awk 'NR==2 {sub(/.*filename=(\\")?/,""); sub(/(\\")?"$/,""); print; exit}' should replace all your seds Commented Jan 21, 2015 at 2:09
  • The nice thing about the ruby solution is that it takes care of all the quotes and escaped quotes. It should not be much more heavyweight than jq and whatever. Commented Jan 21, 2015 at 2:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .