Home Man
Search
Today's Posts
Register

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

Tags
awk, join mulitple lines, sed, shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 10-01-2011
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
# 2  
Old 10-01-2011
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 08:00 AM..
The Following User Says Thank You to ahamed101 For This Useful Post:
andrejm (10-01-2011)
# 3  
Old 10-01-2011
I have Mac OS X and the code doesn't produce the expected result. Output looks the same as input

Thanks
Andrej
# 4  
Old 10-01-2011
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 08: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)
# 5  
Old 10-01-2011
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)
# 6  
Old 10-01-2011
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
# 7  
Old 10-01-2011
Quote:
Originally Posted by andrejm
...the real data obviously contains some special characters that need to be cleared....
Can you post an example of your real data?

tyler_durden
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk to remove lines that do not start with digit and combine line or lines cmccabe Shell Programming and Scripting 3 07-14-2017 08:51 AM
Join lines using sed or awk hench Shell Programming and Scripting 9 10-17-2016 11:44 PM
Join Lines ashanabey Shell Programming and Scripting 4 04-21-2016 10:49 AM
Remove lines between the start string and end string including start and end string Python Dabheeruz Shell Programming and Scripting 1 02-15-2013 11:40 AM
join two lines thailand Shell Programming and Scripting 11 02-10-2012 12:42 PM
Appending string, variable to file at the start and string at end Vaddadi Shell Programming and Scripting 4 03-24-2010 01:31 PM
join 2 lines maxim42 Shell Programming and Scripting 8 03-02-2010 07:22 PM
join lines repinementer Shell Programming and Scripting 2 09-19-2009 02:53 AM
how to join lines glamo_2312 UNIX for Dummies Questions & Answers 8 09-16-2008 04:17 AM
join two lines together tine Shell Programming and Scripting 4 12-12-2003 11:34 AM


All times are GMT -4. The time now is 02:24 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password