Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:


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?

share|improve this question
Does the junk contain newlines? Are there multiple vcards per backup file? – Gilles Jul 16 '11 at 15:43
@Giles No, and yes. – user394 Jul 18 '11 at 15:40
up vote 6 down vote accepted

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
share|improve this answer
One man's naivete is another man's brilliance! :D – user394 Jul 16 '11 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 '11 at 15:48

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'
share|improve this answer
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 '11 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. – Gilles Jul 16 '11 at 16:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.