Unix/Linux Go Back    


UNIX for Dummies Questions & Answers This forum is closed for new posts. Please post beginner questions to learn unix and learn linux in this forum UNIX for Beginners Questions & Answers

Need to combine two lines in a file based on first character of each line in a file

UNIX for Dummies Questions & Answers


 
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 02-25-2016   -   Original Discussion by jayaP
jayaP jayaP is offline
Registered User
 
Join Date: Feb 2016
Last Activity: 12 May 2017, 7:43 AM EDT
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
Unix or Linux Question Need to combine two lines in a file based on first character of each line in a file

Hi,

I have a requirement where I need to combine two lines in a file based on first character of each line in a file.

Please find the sample content of the file below:

Code:
_______________________

Code:
5, jaya, male, 4-5-90, single
smart
6, prakash, male, 5-4-84, married
fair
7, raghavi, female, 12-10-85, married
calm
talented
9, bhaskar, male, 29-12-92, single
studios

________________________

I want the output for this file as
_______________________

Code:

Code:
5, jaya, male, 4-5-90, single-smart
6, prakash, male, 5-4-84, married-fair
7, raghavi, female, 12-10-85, married-calm-talented
9, bhaskar, male, 29-12-92, single-studios

________________________

can you please help me with the shell program that achieve my requirement?

Moderator's Comments:
Need to combine two lines in a file based on first character of each line in a file edit by bakunin: by "use CODE-tags" we didn't mean to write the word "code" but to use the tags, like the ones i edited into your text. Please use them yourself in further posts.

Last edited by bakunin; 02-25-2016 at 08:04 PM..
Sponsored Links
    #2  
Old Unix and Linux 02-25-2016   -   Original Discussion by jayaP
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 15 November 2017, 4:57 AM EST
Location: In the leftmost byte of /dev/kmem
Posts: 5,633
Thanks: 107
Thanked 1,603 Times in 1,180 Posts
You might find it easier to divide your problem into two separate problems which are solvable independently:

1) concatenate all lines not starting with a number to the last line starting with a number, separated by spaces.

2) replace the spaces in the last field with hyphens i.e. "word1 word2" -> "word1-word2".

What may help you is the fact that each line consists of four "fields", separated by comma. Is this only the case in your sample or is this so throughout your data?

Any efforts from your side?

I hope this helps.

bakunin
Sponsored Links
 

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
Program to combine two lines in a file on checking the first character of each line jayaP Shell Programming and Scripting 12 02-26-2016 11:03 AM
Need to combine two lines in a file based on first character of each line in a file jayaP UNIX for Advanced & Expert Users 1 02-25-2016 08:49 PM
Joining lines in TXT file based on first character fuji_s Shell Programming and Scripting 5 08-20-2013 04:19 AM
Combine multiple lines in file based on specific field ratheesh2011 Shell Programming and Scripting 8 07-11-2012 03:39 PM
Merging lines based on occurances of a particular character in a file mohan_tuty Shell Programming and Scripting 2 07-15-2008 02:14 AM



All times are GMT -4. The time now is 11:38 AM.