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.

Combining lines into a single line

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-16-2017
rahulchandak rahulchandak is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 16 June 2017, 12:55 PM EDT
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Combining lines into a single line

i have a file (where the column values are separated by ' and the text can be enclosed in ~) which contains data in form of


Code:
4461,2,~Basic: 
        2 Years/Unlimited Miles

Drivetrain: 
        Gas Engine
        2 Years/Unlimited Miles
        Duramax Engine
        3 Years/Unlimited Miles
        Caterpillar Engine
        3 Years/150,000 Miles

        Manual Trans
        2 Years/Unlimited Miles
        Auto Trans MT643/MT653/MD3060/MD3560
        2 Years/Unlimited Miles
        Auto Trans AT545/2000P/2400P
        3 Years/Unlimited Miles

Corrosion: 
        5 Years/Unlimited Miles~
4457,2,~Basic: 
        2 Years/Unlimited Miles

Drivetrain: 
        Gas Engine
        2 Years/Unlimited Miles
        Duramax Engine
        3 Years/Unlimited Miles
        Caterpillar Engine
        3 Years/150,000 Miles

        Manual Trans
        2 Years/Unlimited Miles
        Auto Trans MT643/MT653/MD3060/MD3560
        2 Years/Unlimited Miles
        Auto Trans AT545/2000P/2400P
        3 Years/Unlimited Miles

Corrosion: 
        5 Years/Unlimited Miles~

now i need to convert the file data in


Code:
4461,2,~Basic:         2 Years/Unlimited Miles Drivetrain:         Gas Engine        2 Years/Unlimited Miles        Duramax Engine        3 Years/Unlimited Miles        Caterpillar Engine        3 Years/150,000 Miles        Manual Trans        2 Years/Unlimited Miles        Auto Trans MT643/MT653/MD3060/MD3560        2 Years/Unlimited Miles        Auto Trans AT545/2000P/2400P        3 Years/Unlimited MilesCorrosion:         5 Years/Unlimited Miles~
4457,2,~Basic:         2 Years/Unlimited MilesDrivetrain:         Gas Engine        2 Years/Unlimited Miles        Duramax Engine        3 Years/Unlimited Miles        Caterpillar Engine        3 Years/150,000 Miles        Manual Trans        2 Years/Unlimited Miles        Auto Trans MT643/MT653/MD3060/MD3560        2 Years/Unlimited Miles        Auto Trans AT545/2000P/2400P        3 Years/Unlimited MilesCorrosion:         5 Years/Unlimited Miles~

Moderator's Comments:
Combining lines into a single line Please use code tags

Last edited by jim mcnamara; 06-16-2017 at 12:53 PM.. Reason: code tags
Sponsored Links
    #2  
Old Unix and Linux 06-16-2017
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 22 October 2017, 7:40 AM EDT
Location: NM
Posts: 11,215
Thanks: 567
Thanked 1,110 Times in 1,024 Posts
Have you tried anything? for a start you can use a Basic: as a flag and only print newlines when your extended newly created line ends line ends

Code:
awk 'FNR==1  {printf("%s ", $0); next }    # print the first line with no newline  and continue reading the next line first time we see Basic:
       /Basic:$/  {print ""}  # print new line because we already came to the end, this is the second time we see Basic:
       Here you print each line with no newline - you code it.
       END{print "" }   # print the last newline   because there are no more line to read
      '  inputfile > outputfile

Sponsored Links
    #3  
Old Unix and Linux 06-16-2017
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 October 2017, 3:27 AM EDT
Location: Aachen, Germany
Posts: 11,481
Thanks: 310
Thanked 3,561 Times in 3,276 Posts
Try also

Code:
awk '/Basic/ && NR > 1 {printf RS} 1; END {printf RS}' ORS="" file

Sponsored Links
Reply

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
Need help combining txt files w/ multiple lines into csv single cell - also need data merge jetsetter UNIX for Dummies Questions & Answers 2 02-04-2013 09:12 AM
Combining lines in to one line i150371485 Shell Programming and Scripting 6 12-14-2012 04:26 AM
PERL:Combining multiple lines to single line Kalaiela Programming 4 04-17-2012 11:20 PM
Combining 2 lines in a file into 1 line MKNENI Shell Programming and Scripting 10 03-25-2012 08:04 AM
Multiple lines in a single column to be merged as a single line for a record Bhuvaneswari Shell Programming and Scripting 1 08-11-2011 03:16 AM



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