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.

Combine multiple lines in single line

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-26-2010
The One The One is offline
Registered User
 
Join Date: Mar 2007
Last Activity: 20 December 2012, 4:36 AM EST
Posts: 46
Thanks: 4
Thanked 0 Times in 0 Posts
Combine multiple lines in single line

This is related to one of my previous post but now with a slight difference: I need the "Updated:" to be in one line as well as the "Information:" on one line as well. These are in multiple lines right now as seen below. These can have 2 or more lines that needs to be in one line.


Code:
System name: xxx_yyy
Information: this is just a sample
file for this testing
today
Updated: This is not
updated
System name: testing_123
Information: another testing
Updated: No new update for this 
system name.
System name: abc_12333
Information: another testing again
and again
Updated: This has been updated
with new information

There are hundred lines with the above pattern.

Result:

Code:
System name: xxx_yyy
Information: this is just a sample file for this testing today
Updated: This is not updated
System name: testing_123
Information: another testing again
Updated: No new update for this system name.
System name: abc_12333
Information: another testing again and again
Updated: This has been updated with new information

Sponsored Links
    #2  
Old Unix and Linux 10-26-2010
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 1 February 2016, 3:35 PM EST
Location: Southern NJ, USA (Nord)
Posts: 4,673
Thanks: 8
Thanked 587 Times in 560 Posts

Code:
sed '
  :loop
  $b
  N
  /: .*\n.*: /{
    P
    s/.*\n//
    b loop
   }
  s/ *\n */ /
  b loop
 '

Narrative: This is what I call a sed looper, reading it's own lies after the first rather than exiting the script at the end to get the next line:
  1. declare a branch target 'loop',
  2. if last line, print and exit,
  3. If the two lines in the buffer each have ': ', print out the first line and go back to 'loop' to get another line.
  4. Remove the linefeed and any adjacent spaces in favor of a single space.
  5. Go back to 'loop' to get another line.
Sponsored Links
    #3  
Old Unix and Linux 10-26-2010
JerryHone JerryHone is offline
Registered User
 
Join Date: Nov 2006
Last Activity: 24 April 2017, 5:26 AM EDT
Location: UK
Posts: 256
Thanks: 1
Thanked 6 Times in 6 Posts
Or with awk...

nawk -F : -f my.awk my.txt

my.awk

Code:
BEGIN {
        line=""
}

{

        if ( $1 == "System name" ||  $1 == "Information" ||  $1 == "Updated" ) {
                if ( length(line) > 0 ) { print line }
                line = $0
        } else {
                line = sprintf("%s %s", line, $0)
        }

}

END {
        print line
}

    #4  
Old Unix and Linux 10-26-2010
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 24 June 2017, 3:40 AM EDT
Location: Amsterdam
Posts: 11,451
Thanks: 493
Thanked 3,299 Times in 2,911 Posts

Code:
awk '/: /{if(NR>1)print p;p=$0;next}{p=p FS $0}END{print p}' infile


Last edited by Scrutinizer; 10-26-2010 at 11:27 AM.. Reason: Removed ^[^:]* since it provided no added security
Sponsored Links
    #5  
Old Unix and Linux 10-26-2010
The One The One is offline
Registered User
 
Join Date: Mar 2007
Last Activity: 20 December 2012, 4:36 AM EST
Posts: 46
Thanks: 4
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by DGPickett View Post
Code:
sed '
  :loop
  $b
  N
  /: .*\n.*: /{
    P
    s/.*\n//
    b loop
   }
  s/ *\n */ /
  b loop
 '

Narrative: This is what I call a sed looper, reading it's own lies after the first rather than exiting the script at the end to get the next line:
  1. declare a branch target 'loop',
  2. if last line, print and exit,
  3. If the two lines in the buffer each have ': ', print out the first line and go back to 'loop' to get another line.
  4. Remove the linefeed and any adjacent spaces in favor of a single space.
  5. Go back to 'loop' to get another line.
you really are a genius! I wanted to learn more in this as im using this kind of text editing frequently. Do you know any reference on this kind of sed function where i can learn more?
Sponsored Links
    #6  
Old Unix and Linux 10-26-2010
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 24 June 2017, 3:40 AM EDT
Location: Amsterdam
Posts: 11,451
Thanks: 493
Thanked 3,299 Times in 2,911 Posts

Code:
sed -n '/:/{x;1!s/\n/ /g;1!p;d;};H;${g;s/\n/ /g;p;}' infile

Sponsored Links
    #7  
Old Unix and Linux 10-26-2010
ctsgnb ctsgnb is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 29 May 2017, 3:44 AM EDT
Location: France
Posts: 2,956
Thanks: 85
Thanked 632 Times in 603 Posts

Code:
echo `sed 's/^\(.*\):/|\1:/' input | tr -d '\n'` | sed 's/^|//' | tr '|' \\n

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
Multiple lines into a single line RickyC9999 Shell Programming and Scripting 4 02-22-2010 02:41 PM
Getting multiple messy lines into one single line sysrenan Shell Programming and Scripting 18 11-15-2009 04:16 PM
Awk multiple lines with 3rd column onto a single line? SoMoney Shell Programming and Scripting 4 12-06-2008 07:59 AM
replacing multiple lines with single line siba.s.nayak Shell Programming and Scripting 3 05-28-2008 02:43 AM
Splitting a single line into multiple lines thanuman Shell Programming and Scripting 4 02-23-2005 03:56 AM



All times are GMT -4. The time now is 09:48 AM.