1834450 Members
2507 Online
110067 Solutions
New Discussion

Merging file lines

 
Abdullatif M. Abdulla
Frequent Advisor

Merging file lines

I have file which has a blank line in every 4 lines. I wnat to merge all lines come before the the blank.
ex:

My name is sam
her name is judy
his name is blabla
its name is cat

My name is sam
her name is judy
his name is blabla
its name is cat

I want the result to be:
My name is sam:her name is judy:his name is blabla:its name is cat
My name is sam:her name is judy:his name is blabla:its name is cat

Please help urgent



10 REPLIES 10
Bharat Katkar
Honored Contributor

Re: Merging file lines

Hi,
I don't know why u have this in your profile:

"I have assigned points to 0 of 64 responses to my questions"

Go thr' this link below:
http://forums1.itrc.hp.com/service/forums/helptips.do?admit=716493758+1085211538437+28353475#33

Hope that helps.
Regards,




You need to know a lot to actually know how little you know
Abdullatif M. Abdulla
Frequent Advisor

Re: Merging file lines

Hi Mr katkar

I couldnt follow please explain more
Abdullatif M. Abdulla
Frequent Advisor

Re: Merging file lines

Helps in the sense of what?

Is this forum for fooling or what?

Keep your nose away if you dont have any answer?

Thanks
Florian Heigl (new acc)
Honored Contributor

Re: Merging file lines

He tried to tell You that in using a free service You are somewhat asked to behave by it's rules.

You don't, and I guess that's why he gave You no answer. (And I agree on that)

I myself don't know the short and professional regex - based way, but You could also do something like

script.ksh < input

with script.ksh like

while read content
do
#set -x
if [ "X$content" = "X" ]
then
echo $result
unset result
else
result="${result}:${content}"
fi

done


addionally learn to say thanks, unless You really enjoy people being unwilling to help You.
yesterday I stood at the edge. Today I'm one step ahead.
Abdullatif M. Abdulla
Frequent Advisor

Re: Merging file lines

Thanks Heigl

I appreciate your help, but i found better answer in sed $ awk nutshell book already.

I have never had the manner of not thanking people who helped me.But, it disturbs me to feel that some one is pretending to be smart like Katkat


Thanks.
Mohanasundaram_1
Honored Contributor

Re: Merging file lines

Hello Mr.Abdullatif,

Good to know you got your answer.

Please cool down, as the text do not portray the real expressions the writer had. There was no wrong intention in that post.

Please do call me on my mobile 050-4683550 if you need to talk to me. The Forum is to help each other anyway.

With regards,
Mohan
Emitac.
Attitude, Not aptitude, determines your altitude
Muthukumar_5
Honored Contributor

Re: Merging file lines

You can do as,

muthu # cat junk
My name is sam
her name is judy
his name is blabla
its name is cat

My name is sam
her name is judy
his name is blabla
its name is cat
muthu # awk '!/^$/ { print $0 }' junk | awk '{ if ( NR % 4 != 0) {printf $0":" } else {printf $0"\n"} }'
My name is sam:her name is judy:his name is blabla:its name is cat
My name is sam:her name is judy:his name is blabla:its name is cat


awk is useful to do this effectively.

hth.
Easy to suggest when don't know about the problem!
Hein van den Heuvel
Honored Contributor

Re: Merging file lines

Hello Mr.Abdullatif,

I realize you have thanked helpers before as you have indeed thanked me: http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=753008

However, this is not immediately obvious to all.
You may want to consider just going with the flow and use the 'points' system to indicated how helpful/useful a particular reply was. It is short and concise, and does not 'waste' bandwith by bringing topic back to the top. It helps some (like me) to decide whether to go into a 'still open' topic. An explicit thank-you remains welcome of course in a 'closing' reply where you, as the author, mark the entire topic as 'done'.

Also, for the curious folks amongst us, you may want to indicate your final solution (for our education), and/or problems with prior solution (like Heigl's script having a leading ":").

fwiw... I'd solve it in perl.
If a trailing ":" is acceptable:

perl -pe 'chop; $_ .= (/^$/)? "\n" : ":"' input

-p = loop and print $_ at end of loop
chop = take of last character from $_ (newline)
$_ .= = append the following to $_
($/^$) = empty line?
? "\n" = if empty add a newline
: ":" = if not then add a colon


As specified:

perl -ne 'if (/^$/){chop $line; print "$line\n"; $line=""} else {chop; $line .= $_ . ":"} ' input
Bharat Katkar
Honored Contributor

Re: Merging file lines

Mr. Abdulla,
It was for your betterment i gave you that advice and honestly i was not able to answer your tricky question in limited time i had and i was curious about the replies that will be coming up.
Now when you go on inetractive by assigning points and making others realize the depth of thier solution they can further help you better.
There was nothing sarcastic in by reply which made you so irritating. Well, it happens when person is in problem start doubting everybody and in negative way.

My intention was to speed up the responses to your question thereby getting you quick help.

1. "Helps in the sense of what?

Is this forum for fooling or what?

Keep your nose away if you dont have any answer? "

Ans.. I had already kept my nose out because i know my limits and depth of knowledge. Secondly even if i was wrong or i had misbehaved how can you directly blame the entire forum that shows how week you are in concluding things.

2. "I have never had the manner of not thanking people who helped me.But, it disturbs me to feel that some one is pretending to be smart like Katkar."

Ans.. DOn't get disturb without knowing the facts and for the later part i have already told you how fast and wrong you are in your conclusion.

I am helpless if you are not able to understand my intentions.

I wish you all the best.
Bharat



You need to know a lot to actually know how little you know
Girish_17
Regular Advisor

Re: Merging file lines

Abdulla,
GO thr' this thread below :
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=553711

Bharat had actively helped you there and it's unfortunate see your comments in this thread. He tried to help you out there and you even didn't care to give any feedback on it, forget about assiging points ( it's not at all compulsory)
Girish....