0

I have a csv file with some unintended line breaks. The file looks more or less like this:

col1; col2; col3; col4
 1a;   1b;   1c;  1d   
 2a;   2b;   2c   
;2d                   # this should be in the row above 
 3a;   3b;   3c;  3d

I would like to concatenate each line which is starting with ; with the line before and then remove it. How can I do this?

  • Run through the file in a loop, if a line starts with ;, place it into a variable, which will be either empty or that value, reset to empty each time not found, then concatenate the variable to the line and print it it out, to sdout or file. – Lizardx Apr 29 at 22:49
1

The following sed script might be what you need:

sed -n '$p;N;s/[[:blank:]]*\n[[:blank:]]*;/;/;P;D' csv_file

Broken down for explanation:

$p;      # at last line of stream, just print it
N;       # append the next line from input so that we always consider two lines at a time ...
s/[[:blank:]]*\n[[:blank:]]*;/;/; # then replace `\n;` (and any leading and trailing blanks) with just `;`, and ...
P;       # print only the _first_ of the two lines present in memory and ...
D        # then delete it and read one new line if memory becomes empty

Note that the first command, $p, actually prints something only when total number of input lines is odd, because the rest of the script ensures that there are always two lines from the input stream in memory

2
$ sed -e '
    :loop
       $!N
       s/\n;/;/
    tloop
    P;D
 ' file.csv

Keep rwo lines in the pattern space at any time. Should the semicolon be seen at the beginning of the next line, chop off the newline and loop back to read in the next line into the pattern space.

When the next line is not with semicolon, get outta from loop, print upto the first newline, remove that portion and go back and append in the next line to the patter space.

1

How about :

awk -F ';' '{while (NF < 4) {getline nextline; $0 = $0 nextline}} 1' file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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