Note: In the question's current form, a sed
solution is feasible - this was not the case originally, where the last ;
-separated field of the joined lines needed transforming as a whole, which prompted the awk
solution below.
Joining lines 7 and 8 as-is, merely by removing the line break between them, can be achieved with this simple sed
command:
sed '7 { N; s/\n//; }' file.csv
awk
solution:
awk '
BEGIN { FS = OFS = ";" }
NR==7 { r = $0; getline; sub(/^"",,/, ""); $0 = r $0 }
1
' file.csv
Judging by the OP's comments, an additional problem is the presence of CRLF line endings in the input. With GNU Awk or Mawk, adding RS = "\r\n"
to the BEGIN
block is sufficient to deal with this (or RS = ORS = "\r\n"
, if the output should have CRLF line endings too), but with BSD Awk, which only supports single-character input record separators, more work is needed.
BEGIN { FS = OFS = ";" }
tells Awk to split the input lines into fields by ;
and to also use ;
on output (when rebuilding the line).
Pattern NR==7
matches input line 7, and executes the associated action ({...}
) with it.
r = $0; getline
stores line 7 ($0
contains the input line at hand) in variable r
, then reads the next line (getline
), at which point $0
contains line 8.
sub(/^"",,/, "")
then removes substring "",,
from the start of line 8, leaving just 8-9012
.
$0 = r $0
joins line 7 and modified line 8, and by assigning the concatenation back to $0
, the string assigned is split into fields by ;
anew, and the resulting fields are joined to form the new $0
, separated by OFS
, the output field separator.
Pattern 1
is a common shorthand that simply prints the (possibly modified) record at hand.