Multline output variable in bash for csv file

Hello,

I have a bash script that I want to output the variables into a .csv file.  Most of the variable I can have the variable output on one line but I'm having a problme with variables that are multiline. The two variables are passed the process id of a process and the process listing contains new line characters. I thought if I stripped out the newline characters that you could assign it to the variable but it complains "No such file or directory in the variable substition".  The other variable is just netstat -an | grep <port>.  I don't know how to use arrays in bash but ideally i'd like to be able to keep the newlines in the csv file, if it's possible, since i'm sticking with comma as the separator. Any help would be really appreciated. Hitting my head off the wall!

VAR1="$(ps -ef | grep 27656 | tr '\n' ' ' | sed -e '/s   */g')"
(printf '%s\n' $VAR1)
lolaferrariAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nociSoftware EngineerCommented:
"No such file or directory" probably means there is a space after the VAR1= assignment... (Then it can find the program after the assignment VAR1="" ).
and you may want to reconsidder  using (printf....) as that would chop the line into separate words.

$ VAR1= "asdfasdf"
bash: asdfasdf: command not found
$ VAR1="asdf"
$ 

Open in new window

0
lolaferrariAuthor Commented:
Thanks noci. I double checked  the assignment to the var and there wasn't a space but I changed a few things and it's working now. Instead of using var="$( )" notation I've used `` notation but I don't think that should really matter and I used echo -e (thanks for the tip about printf)  and I removed the trailing space characters which I think was the problem.

I have another question though. How can you use sed to remove mutiple space charaters. I'd like to remove all the extra spaces to end up with just single spaces.
0
tel2Commented:
Hi lolaferrari,

> "How can you use sed to remove mutiple space charaters. I'd like to remove all the extra spaces to end up with just single spaces."
    sed 's/  */ /g' file
Note the 2 spaces before the '*'.
This means replace each occurrence of 1 space followed by 0 or more spaces with 1 space.

You could alternatively do this:
    sed 's/   */ /g' file
Note the 3 spaces before the '*'.
This means replace each occurrence of 2 spaces followed by 0 or more spaces with 1 space.  This might be slightly more efficient because it doesn't bother replacing a 1 space with 1 space.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Bash

From novice to tech pro — start learning today.