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


 
Thread Tools Search this Thread
# 1  
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:
Mod Comment 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 07:04 PM..
# 2  
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
 

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Program to combine two lines in a file on checking the first character of each line
jayaP
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: _______________________ 5, jaya, male, 4-5-90, single smart 6, prakash, male, 5-4-84, married fair 7, raghavi, female,...... Shell Programming and Scripting
12
Shell Programming and Scripting
Need to combine two lines in a file based on first character of each line in a file
jayaP
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: _______________________ 5, jaya, male, 4-5-90, single smart 6, prakash, male, 5-4-84, married fair 7, raghavi,...... UNIX for Advanced & Expert Users
1
UNIX for Advanced & Expert Users
Joining lines in TXT file based on first character
fuji_s
Hi, I have a pipe delimeted text file where lines have been split over 2 lines and I need to join them back together. For example the file I have is similar to the following: aaa|bbb |ccc ddd|eee fff|ggg |hhh I ideally need to have it looking like the following aaa|bbb|ccc ddd|eee...... Shell Programming and Scripting
5
Shell Programming and Scripting
Combine multiple lines in file based on specific field
ratheesh2011
Hi, I have an issue to combine multiple lines of a file. I have records as below. Fields are delimited by TAB. Each lines are ending with a new line char (\n) Input -------- ABC 123456 abcde 987 890456 7890 xyz ght gtuv ABC 5tyin 1234 789 ghty kuio ABC ghty jind 1234 678 ght ...... Shell Programming and Scripting
8
Shell Programming and Scripting
Merging lines based on occurances of a particular character in a file
mohan_tuty
Hi, Is there any way to merge two lines based on specific occurance of a character in a file. I am having a flat file which contains multiple records. Each row in the file should contain specified number of delimiter. For a particular row , if the delimiter count is not matched with...... Shell Programming and Scripting
2
Shell Programming and Scripting