How to split vCards lines

Related to How to join vCards lines, vCard does an unusual sort of line splitting: If a line has greater than 75 personalities, insert a "CR, LF, space" series. Hence the adhering to line:


needs to be divided right into the adhering to lines:


You can not simply insert the line split series every 75 personalities, due to the fact that after that there would certainly be greater than 75 personalities per line once more, and also you can not simply insert after counting 75 personalities due to the fact that it need to just be put if the line is longer than 75 personalities. One means to do it is to duplicate the adhering to command till the input no more adjustments:

sed -e 's/^\(.\{75\}\)\([^\r]\)/\1\r\n \2/' < file | sed -e '...' | ...

That is clearly not mosting likely to collaborate with indeterminately lengthy lines, and also is badly ineffective. Just how would certainly you do this type of substitute?

2022-06-07 14:31:44
Source Share
Answers: 2

To your manuscript you just require to add 2 commands: P (print approximately the first newline) and also D (delete approximately the first newline and also repeat if there is any kind of message left).

sed -e '{ s/\(.\{75\}\)\(.\)/\1\n \2/; P; D }' file
2022-06-07 14:59:44

This is a not - so - wonderful effort to address the trouble:

#!/usr/bin/awk -f

    if (length($0) > 76) {
        printf("%s\r\n ", substr($0, 1, 75));
        $0 = substr($0, 76);
        while (length($0) > 74) {
            printf("%s\r\n ", substr($0, 1, 74));
            $0 = substr($0, 75);
    print $0;
2022-06-07 14:52:19