Join lines with the same start string

I have the text like:

DN11-001 Thats the first line which needs to be
DN11-001 joined with the second line and also to
DN11-001 the third line as they all begin with the same
DN11-001 document number.
DN11-002 The number of lines differ
DN11-002 among the documents.
DN11-005 It can also be just one line.

to be modified in a way that all text with the same start string is in one row:

DN11-001 Thats the first line which needs to be joined with the second line and also to the third line as they all begin with the same document number.
DN11-002 The number of lines differ among the documents.
DN11-005 It can also be just one line.

Thank you.
Andrej

Try this...

 awk '{if(val==$1){gsub(val,"");printf $0}else{if(NR>1)print "";val=$1;printf $0}}END{print ""}' input_file

If in solaris, use nawk.

--ahamed

1 Like

I have Mac OS X and the code doesn't produce the expected result. Output looks the same as input

Thanks
Andrej

sed -n 's/DN[0-9]\{2\}-[0-9]\{3\} //p' input|awk '{ORS="";gsub("\\.",".\n\r");print}'
1 Like
$
$
$ cat f9
DN11-001 Thats the first line which needs to be
DN11-001 joined with the second line and also to
DN11-001 the third line as they all begin with the same
DN11-001 document number.
DN11-002 The number of lines differ
DN11-002 among the documents.
DN11-005 It can also be just one line.
$
$
$ perl -lne '/^(.*?) (.*)$/;
             if (! defined $x{$1}) {print $k,$v while ($k,$v)=each %x; %x=()}
             $x{$1}.=" $2";
             END {print $k,$v while ($k,$v)=each %x}' f9
DN11-001 Thats the first line which needs to be joined with the second line and also to the third line as they all begin with the same document number.
DN11-002 The number of lines differ among the documents.
DN11-005 It can also be just one line.
$
$
$

tyler_durden

1 Like

All three solutions work, thank you! The problem is that it works only on sample date, the real data obviously contains some special characters that need to be cleared. I guess I need to check which characters are not allowed?

Andrej

Can you post an example of your real data?

tyler_durden

awk '{if(val==$1){gsub(val,"");printf $0}else{if(NR>1)print "";val=$1;printf $0}}END{print ""}' input_file

can someone explain the above command. how it will work

Thank you, I thought nobody would care about it anymore! Attached is a sample of real data.

Andrej

Hi,

I want to know how the command will work with the above data.. it will be good if u explain the command and how it will join.

awk '{if(val==$1){gsub(val,"");printf $0}else{if(NR>1)print "";val=$1;printf $0}}END{print ""}' input_file

RealData.txt has \r. Use the below code to replace it with \n

sed -i 's/\r/\n/g' RealData.txt

And then execute the commands we have given!

--ahamed

---------- Post updated at 11:26 AM ---------- Previous update was at 11:24 AM ----------

btw, I have no Mac. I do all these in "back | track" -- Ubuntu Linux

--ahamed

1 Like