3

I'm trying to get my nokia backup file into an acceptable vcard format. It has valid vcard strings that appear in between binary data. For example:

!@#JUNKCHARACTERS!@#!@BEGIN:VCARD
VERSION:2.1
N:Jimmy;;;;
TEL;VOICE:4445551212
END:VCARD!@#JUNKCHARACTERS!@#!@

I've been working with a hex editor to do some find and replace, but even then it's tedious. Can I use grep or some other command-line tool to pull out all the strings between BEGIN:VCARD and END:VCARD?

2
  • Does the junk contain newlines? Are there multiple vcards per backup file? Jul 16, 2011 at 15:43
  • @Giles No, and yes.
    – user394
    Jul 18, 2011 at 15:40

2 Answers 2

6

I'm sorry if I'm being naive, but can't you just:

strings binary-file > binary-strings-output.txt

And then post proccess the strings output? Along the lines of:

awk '/BEGIN:VCARD/,$NF ~ /END:VCARD/' binary-strings-output.txt
2
  • 1
    One man's naivete is another man's brilliance! :D
    – user394
    Jul 16, 2011 at 15:41
  • @user394 Thank you :) You can also use awk to get just the vcard strings. The syntax above might need some work, though.
    – Vitor Py
    Jul 16, 2011 at 15:48
3

Here's a start: print only the text between BEGIN:VCARD and END:VCARD. This assumes there's always a newline between the END:VCARD and the BEGIN:VCARD.

sed -n -e 's/.*[^A-Z]BEGIN:VCARD/BEGIN:VCARD/' \
       -e 's/END:VCARD[^A-Z].*/END:VCARD/' \
       -e '/BEGIN:VCARD/,/END:VCARD/p'
2
  • If/when you have the time, can you write more about how this sed syntax works? I'm trying to understand it but I haven't been able to. I tried achieving the same with awk range syntax.
    – Vitor Py
    Jul 16, 2011 at 15:55
  • @VitorBraga The s commands remove the undesired bits on the BEGIN and END lines. Then the p command prints the bits in each range from BEGIN to END. The M,N syntax (note my edit) is similar to Awk's , and Perl's .. for line ranges. Jul 16, 2011 at 16:04

You must log in to answer this question.

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