Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

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
add comment

2 Answers 2

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 Jul 16 '11 at 15:48
add comment

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 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
add comment

Your Answer

 
discard

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.