Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Join lines with the same start string

Shell Programming and Scripting


Tags
awk, join mulitple lines, sed

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-01-2011   -   Original Discussion by andrejm
andrejm andrejm is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 2 October 2011, 3:37 PM EDT
Posts: 9
Thanks: 12
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old Unix and Linux 10-01-2011   -   Original Discussion by andrejm
ahamed101's Unix or Linux Image
ahamed101 ahamed101 is offline Forum Advisor  
root is god!!!
 
Join Date: Sep 2008
Last Activity: 19 October 2016, 5:02 AM EDT
Location: San Jose, CA
Posts: 1,910
Thanks: 54
Thanked 488 Times in 481 Posts
Try this...

Code:
 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

Last edited by ahamed101; 10-01-2011 at 09:00 AM..
The Following User Says Thank You to ahamed101 For This Useful Post:
andrejm (10-01-2011)
Sponsored Links
    #3  
Old Unix and Linux 10-01-2011   -   Original Discussion by andrejm
andrejm andrejm is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 2 October 2011, 3:37 PM EDT
Posts: 9
Thanks: 12
Thanked 0 Times in 0 Posts
I have Mac OS X and the code doesn't produce the expected result. Output looks the same as input

Thanks
Andrej
    #4  
Old Unix and Linux 10-01-2011   -   Original Discussion by andrejm
ltomuno ltomuno is offline
Registered User
 
Join Date: Jul 2011
Last Activity: 25 October 2011, 5:44 AM EDT
Posts: 41
Thanks: 0
Thanked 12 Times in 11 Posts

Code:
sed -n 's/DN[0-9]\{2\}-[0-9]\{3\} //p' input|awk '{ORS="";gsub("\\.",".\n\r");print}'


Last edited by Franklin52; 10-02-2011 at 09:30 AM.. Reason: Please use code tags for data and code samples, thank you
The Following User Says Thank You to ltomuno For This Useful Post:
andrejm (10-01-2011)
Sponsored Links
    #5  
Old Unix and Linux 10-01-2011   -   Original Discussion by andrejm
durden_tyler's Unix or Linux Image
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
 
Join Date: Apr 2009
Last Activity: 9 September 2017, 1:30 PM EDT
Posts: 2,083
Thanks: 21
Thanked 383 Times in 346 Posts

Code:
$
$
$ 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
The Following User Says Thank You to durden_tyler For This Useful Post:
andrejm (10-01-2011)
Sponsored Links
    #6  
Old Unix and Linux 10-01-2011   -   Original Discussion by andrejm
andrejm andrejm is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 2 October 2011, 3:37 PM EDT
Posts: 9
Thanks: 12
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #7  
Old Unix and Linux 10-01-2011   -   Original Discussion by andrejm
durden_tyler's Unix or Linux Image
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
 
Join Date: Apr 2009
Last Activity: 9 September 2017, 1:30 PM EDT
Posts: 2,083
Thanks: 21
Thanked 383 Times in 346 Posts
Quote:
Originally Posted by andrejm View Post
...the real data obviously contains some special characters that need to be cleared....
Can you post an example of your real data?

tyler_durden
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
join 2 lines maxim42 Shell Programming and Scripting 8 03-02-2010 08:22 PM
Join the lines Beginer0705 Shell Programming and Scripting 8 09-30-2009 03:42 AM
join lines repinementer Shell Programming and Scripting 2 09-19-2009 03:53 AM
how to join lines glamo_2312 UNIX for Dummies Questions & Answers 8 09-16-2008 05:17 AM
join two lines together tine Shell Programming and Scripting 4 12-12-2003 12:34 PM



All times are GMT -4. The time now is 10:21 PM.