DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Convert Sqlite DB To TXT Cookies Format

04.29.2009
| 6111 views |
  • submit to reddit
        
#!/bin/bash
set -e

# Replace a column field for each line in stdin
replace_column_value() {
    awk "BEGIN{OFS=FS=\"$1\"} {\$$2 = ((\$$2==\"$3\")?\"$4\":\"$5\"); print}"
}

# Convert a Mozilla sqlite cookie file to the old txt format
mozilla_cookies_sqlite2txt() {
    echo "# Netscape HTTP Cookie File"
    echo ".mode tabs
          SELECT host, (host GLOB '.*'), path, 'FALSE', expiry, name, value
          FROM moz_cookies;" | sqlite3 "$1" | \
          replace_column_value "\t" 2 "1" "TRUE" "FALSE" | \
          recode iso8859-1..utf-8 | tr -d '[áéíóúàèìòù]'
}

FILE=$1
mozilla_cookies_sqlite2txt "$FILE"