How to append words at the end of first line

Hi Unix gurus

This is my first post here. I have a file which looks like this:

string1,string2
,string3
,string4
...
...

I need to append all words below first line to the first line, ie

string1,string2,string3,string4,...
Bear in mind that it is not known how many lines follow the first line.

Thanks
Lucas

Something like

tr -s '\n' ',' < t | sed -e "s/,$//g"

Try this

xargs echo<filename

To store the output in new file

xargs echo<input_filename>output_filename

Hi Dhruva

Thanks for the solution, it works! I have one concern though, your code yields the following result:

string1,string2 ,string3 ,string4

How can I remove the spaces?

Thanks

Lucas

Hi Vino, thanks for the reply.

Can you please explain how to use this command? When I try it it returns nothing.

Lucas

In the code posted by #vino "t" is the input_file.

Did you try like that ?

Yes I did, but there's no output:

root@sun6 # cat testfile
string1,string2
,string3
,string4

root@sun6 #tr -s '\n' ',' < testfile | sed -e "s/,$//g"
root@sun6 #

Thanks

Lucas

What does tr -s '\n' ',' < testfile give ?

Seems to be doing what I want, but it also appends root prompt ?

root@sun6 # tr -s '\n' ',' < testfile
string1,string2,string3,string4,root@sun6 #

it is because

"tr -s '\n' ',' < testfile" removes the newline on which "sed" is searching
ie "Output line from tr doesn't have newline character at the end"

So we can go for xargs as follows which may help you

xargs < testfile | sed -e "s/,$//g"

xargs < testfile | sed -e "s/,$//g" gives correct result but have unwanted spaces in the output:

root@sun6 # xargs < testfile | sed -e "s/,$//g"
string1,string2 ,string3 ,string4

Thanks all for your help, used

xargs < testfile | sed 's/ //g'

and it works 100%.