ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am writing a linux script to write some values to an output file.
The output file has to have the values written in a column format.
So I have the following command in my script:
echo $value1 ' ' $value2 ' ' $value3 >> $output_file
The issue here is that the values in value1, value2, value3 are multiple line values.
For example,
value1= 'line1
line2
line3'
Same goes for value2 and value3.
So, when my script runs the command (echo $value1 ' ' $value2 ' ' $value3 >> $output_file), the data gets written to the output file as,
value1.line1
value1.line2
value1.line3 value2.line1
value2.line2
value2.line3
:
:
I would like the values to be written as:
value1.line1 value2.line1 value3.line1
value1.line2 value2.line2 value3.line2
value1.line3 value2.line3 value3.line3
The number of lines in each value can vary and the number of lines in value1 can be different from the number of lines in value2.
Also, there is a possibility that the values may have comma or tabs within the line data. So, I cannot write to a csv file.
Could you guys please help me figure out the right set of commands for writing this?
This is as I expected, but as you can see not anywhere near what your output looks like.
If you have additional lines in your script you may need to show them so we can see why your output is different.
This is as I expected, but as you can see not anywhere near what your output looks like.
If you have additional lines in your script you may need to show them so we can see why your output is different.
Thanks for your response!
You would get the same response as I got if you use: echo "$x" " "$x2"
But in my case, I actually need the output to be:
line1 line1
line2 line2
line3 line4
Basically, I need x and x2 in column format.
Could you please help me figure out how can I do that?
Thanks for your response!
You would get the same response as I got if you use: echo "$x" " "$x2" But in my case, I actually need the output to be:
line1 line1
line2 line2
line3 line4
Basically, I need x and x2 in column format. Could you please help me figure out how can I do that?
Sure..can you post what you have written so far??? We're always happy to help, but we aren't going to write a script for you.
@syg00 ... -e won't help as there were no quotes around the variables
@OP ... yes I knew I needed quotes but you did not show any in your post As TB0ne has said, you have shown that you can echo a variable but not that you have made any attempt to split the data
as you wish to display it. May I suggest you look at arrays or something like sed or awk to split the data.
I certainly know what I would do in this case! I'd format the output as HTML!
"Just output a <table> tag, with <tr> and <td> and all that goodness, and rely upon a web-browser to most-elegantly do all the dirty work for me. In fact, if I am careful to create a file with a .html extension, I can pretty much guarantee that any modern operating system will "just do the Right Thing" when the user double-clicks on that file.
Hmmmm .. I am new here, so not sure if I am interpreting you right .. was that sarcasm?
Yep ... pretty much
Whilst it was a little tongue in cheek, I think the point is that you didn't really resolve your issue, but instead used a method which in this case seemed
to give you the desired result. Now obviously you know the format of the data so if this solution has worked for you then that is fine, but if you really
want to learn how to get the desired affect without relying on 'maybe' you might wish to dig a little further.
Whilst it was a little tongue in cheek, I think the point is that you didn't really resolve your issue, but instead used a method which in this case seemed
to give you the desired result. Now obviously you know the format of the data so if this solution has worked for you then that is fine, but if you really
want to learn how to get the desired affect without relying on 'maybe' you might wish to dig a little further.
I just needed a final report that was readable by my end-user. This seemed like a good enough solution
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.