LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-04-2015, 09:38 PM   #1
nptfdo
LQ Newbie
 
Registered: Jul 2015
Posts: 5

Rep: Reputation: Disabled
How to write columns with multiple line outputs


Hello techies,

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?

Thanks for your help!
 
Old 07-05-2015, 03:12 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
You will need to supply more information as I am unable to replicate your issue?

Assuming a bash script, here is the example and output I receive:
Code:
#!/usr/bin/env bash

x='line1
line2
line3'
x2='line1
line2
lin4'

echo $x''$x2

#output below
line1 line2 line3line1 line2 lin4
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.
 
Old 07-05-2015, 03:22 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Try "echo -e ..." grail
 
Old 07-05-2015, 10:59 AM   #4
nptfdo
LQ Newbie
 
Registered: Jul 2015
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
You will need to supply more information as I am unable to replicate your issue?

Assuming a bash script, here is the example and output I receive:
Code:
#!/usr/bin/env bash

x='line1
line2
line3'
x2='line1
line2
lin4'

echo $x''$x2

#output below
line1 line2 line3line1 line2 lin4
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?
 
Old 07-05-2015, 11:15 AM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by nptfdo View Post
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.
 
Old 07-05-2015, 07:59 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
@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.

Last edited by grail; 07-06-2015 at 08:26 PM.
 
Old 07-05-2015, 08:54 PM   #7
nptfdo
LQ Newbie
 
Registered: Jul 2015
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks guys!

I tried a few ways and ended up writing the output to an HTML table. That way I was able to preserve the multiple lines in each column.
 
Old 07-06-2015, 10:32 AM   #8
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
tput anyone?
 
Old 07-06-2015, 01:44 PM   #9
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939
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.
 
Old 07-06-2015, 05:09 PM   #10
nptfdo
LQ Newbie
 
Registered: Jul 2015
Posts: 5

Original Poster
Rep: Reputation: Disabled
Hmmmm .. I am new here, so not sure if I am interpreting you right .. was that sarcasm?
 
Old 07-06-2015, 08:26 PM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
Originally Posted by nptfdo View Post
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.
 
Old 07-06-2015, 08:52 PM   #12
nptfdo
LQ Newbie
 
Registered: Jul 2015
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
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.
I just needed a final report that was readable by my end-user. This seemed like a good enough solution
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
MPD with multiple outputs with multiple users worzel666 Linux - Software 1 05-31-2014 09:33 PM
[SOLVED] How to write a script/command line that runs through multiple sub-directories? rvsmith Linux - Newbie 4 06-12-2012 07:21 AM
[SOLVED] python, trying to write multiple variables to a file on single line fur Programming 4 02-04-2012 03:38 PM
command to write on first line of a file (as a heading for columns) in linux/solaris. Sha_unix Linux - Newbie 9 11-23-2011 02:14 PM
how do I write a script using sed to delete the first and last line in multiple files Rikki D Programming 4 05-02-2008 11:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration