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.

vCard uses a special way to split long lines: At 75 characters, insert a DOS newline and a space. Joining therefore means to replace all occurrences of the sequence "CR, LF, space" with an empty string and otherwise leave the file alone. The simplest way I've found so far (using sed multi-line search and replace) is this:

sed -n '1h;1!H;${;g;s/\r\n //g;p;}' contacts.vcf

That's rather unreadable. Is there an easier way?

Edit: Resulting implementation based on Peter.O's answer.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

UPDATE: This awk script may be more what you are looking for:

awk -vRS='\r\n ' -vORS= 1 contacts.vcf 

(original post)
This perl script works, though it is actually longer, even when sed is spaced out a bit; and it is quite obviously logically very similar to sed. Perhaps perl reads the file into memory faster(?), as it doesn't have the is it, or is it not 1st line? to deal with...

  perl -e 'undef $/; $_=<>; s/\r\n //g; print' contacts.vcf 
#          |         |      |           |
#          ignore    get    substitute  print
#          newlines  all    as needed   result
#          |         |      |           |
# sed -n '1h; 1!H; ${g;     s/\r\n //g; p}' contacts.vcf  

On the other hand: not to be facetious, but if you can't read a sed script easily and it works, just make it readable. I can't read any sed script like that! The one-liner syndrome is simply not suited to sed scripts which go beyond simple substitution... sed is more like a text assembly language than a high level scripting language... perl is rather cryptic too, but it tends to do big things with its terse syntax.

sed -n '
    1h            # if 1st line, copy the pattern space to the hold space
    1!H           # if NOT 1st line, append the pattern to the hold space
    ${            # if this is the last line
      g           # overwrite current pattern with accumulated patterns from hold space
      s/\r\n //g  # make required substitutions
      p           # print the final result. 
    }' contacts.vcf
share|improve this answer

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.