Operating System - HP-UX
1828023 Members
1849 Online
109973 Solutions
New Discussion

How to join multiple lines into single line?

 
SOLVED
Go to solution
Phuc Nguyen_1
Advisor

How to join multiple lines into single line?

I have a case statement that appends a line of string to a file, and each case appends in a new line. I need to join all the lines into a single line with a space and the valuse of $Z (a pre-define variable) in between every line. The number of line is unknow because this will depends on the number of time the while loop run for the case, and the number of characters of each line is also unknown.

I know that this can be done with sed or awk, but I am newbie so not very familiar with these utilities. Any help is appreciated.

Thank you

8 REPLIES 8
Steven Sim Kok Leong
Honored Contributor

Re: How to join multiple lines into single line?

Hi,

If you don't like perl, then use the echo command to do the trick:

# echo `cat $MULTIPLE_LINES_TO_JOIN_INTO_ONE`

$MULTIPLE_LINES_TO_JOIN_INTO_ONE is a variable which contains the filename to cat and subsequently contents echo'ed.

Hope this helps. Regards.

Steven Sim Kok Leong
SHABU KHAN
Trusted Contributor

Re: How to join multiple lines into single line?

Hi,

Do this:

sed 's/$/stringyouwouldliketoinsert/' testfile | tr -s "\n" " " > testfile.out

Will insert the variable at the end of the each line and will strip all new lines to one space.

Hope this helps !

P.S. cat filename | tr -d "\n" will strip all new lines in a file
Thanks,
Shabu
John Carr_2
Honored Contributor

Re: How to join multiple lines into single line?

Hi

the last tr command does not work correctly on control characters I think it should have been

cat filename | tr -d '[\012]' > newfile

regards
John
John Carr_2
Honored Contributor

Re: How to join multiple lines into single line?

Hi

just checked it and this works

#
# cat /tmp/testfile
date
date
date
date
date
date
date
#
# sed 's/$/ my_defined_variable /' /tmp/testfile | tr -d '[\012]'
date my_defined_variable date my_defined_variable date my_defined_variable date my_defined_variable date my_defined_variable date my_defined_variable date my_defined_variable #
#

cheers
John.
Phuc Nguyen_1
Advisor

Re: How to join multiple lines into single line?

Thanks to those that responded.
Mr. Carr and Mr. Khan solution accomplish 90% of what I wanted to do, but the value of my pre-define variable did not get inserted into the outfile. Example, Z="myvaluehere", when I do:
%sed 's/$/O/' ...
the O gets inserted in the outfile, not the string myvaluehere.
Perhaphs I am doing something wrong, maybe my syntax is incorrect?

Phuc Nguyen_1
Advisor

Re: How to join multiple lines into single line?

I made a typo in to previous post.
Correction:
Example, Z="myvaluehere", when I do:
%sed 's/$/Z/' ...
the Z gets inserted in the outfile, not the string myvaluehere.
H.Merijn Brand (procura
Honored Contributor
Solution

Re: How to join multiple lines into single line?

Interpolation issues: single-m vs. double quotes

Change
% sed 's/$/Z/' ...
To
% sed 's/$/'"$Z"'/' ...
Enjoy, Have FUN! H.Merijn
SHABU KHAN
Trusted Contributor

Re: How to join multiple lines into single line?

Hi,

Procura's posting should help solve the remaining 10% of your problem.

PS. N/A for this one..
Thanks,
Shabu