LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-23-2009, 08:29 PM   #1
DeepSeaNautilus
Member
 
Registered: Jul 2008
Posts: 65

Rep: Reputation: 15
Storing table in variable


Code:
cat fileA
output:
Code:
1 one
2 two
3 three
But if I assign the output of cat fileA command to variable a and then echo the content of variable a, the newline characters are replaced by blanks, which I don't want, because I want to use the variable to insert the table into another file and for that it must keep the original newline characters.
Code:
a=`cat fileA`
echo $a
The output is this:
Code:
1 one 2 two 3 three
How can I fix this?

Last edited by DeepSeaNautilus; 04-02-2009 at 05:37 PM.
 
Old 03-24-2009, 12:17 PM   #2
bgoodr
Member
 
Registered: Dec 2006
Location: Oregon
Distribution: RHEL[567] x86_64, Ubuntu 17.10 x86_64
Posts: 221

Rep: Reputation: 36
Quote:
Originally Posted by DeepSeaNautilus View Post
I want to use the variable to insert
the table into another file and for that it must keep the original
newline characters.
The backtick operator has that "feature", if you can call it a
feature. You didn't describe what you really want to do other than
the above sentence so we are left to guess, but here is one way to get
at a particular line in a file and store it into a variable:

Code:
a=`cat fileA | grep three`
echo $a
By the way, you had $A instead of $a in your script, and that won't
work (you will get empty output from the echo because $A was not set
but $a was set.)

There is also the $() operator which works under Linux's sh and bash
shells, but does not work on all UNIX's Bourne shells. But since this
is a Linux forum, and I suspect you are on Linux, you could write the
above as:

Code:
a=$(cat fileA | grep three)
echo $a
The $(...) syntax has the advantage in that it can be nested, while
the backtick expression, `...`, can't for obvious reasons.

bg
 
Old 03-30-2009, 11:47 PM   #3
DeepSeaNautilus
Member
 
Registered: Jul 2008
Posts: 65

Original Poster
Rep: Reputation: 15
Yes, that was a typing mistake, I meant echo $a, instead of echo $A. I am correcting it right now.

a=`cat fileA | grep three` will only assign the third row of the table (since that is the only row where "three" pattern appears) to variable a instead of assigning the whole table ot it, wich is what I want.

Last edited by DeepSeaNautilus; 03-30-2009 at 11:53 PM.
 
Old 04-02-2009, 05:33 PM   #4
DeepSeaNautilus
Member
 
Registered: Jul 2008
Posts: 65

Original Poster
Rep: Reputation: 15
I am tring to store several tables into variables so I can then replace a pattern inside a template file.

template.txt:
Code:
text
...
text
pattern1
text1
...
text2
pattern2
text1
...
text2
pattern3
suppose fileA fileB and fileC are tables
I want to store those three tables into a variable so I can then replace patternx patterns with the content of the table with the following comands:
Code:
a=`cat fileA`
b=`cat fileB`
c=`cat fileC`

sed "s/pattern1/"$a"/g
" template.txt | "s/pattern2/"$b"/g" | "s/pattern3/"$c"/g" > output.txt
Right now I have solved this problem without needing to store the files into variables, but It would be good to know how can this be done so I can make my script faster.

Last edited by DeepSeaNautilus; 04-02-2009 at 05:36 PM.
 
Old 04-02-2009, 08:08 PM   #5
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
In bash putting $a in double quotes preserves the newlines
echo "$a"
1 one
2 two
3 three

But the newlines in "$a" will cause sed to fail:
Code:
echo something | sed "s/something/$a/"
sed: -e expression #1, char 17: unterminated `s' command
A way around this is to replace the newlines in "$a" with the string '\n' which GNU sed will output as a newline.
Code:
anew=$(echo "$a" | sed ':a N;s/\n/\\n/;ta')
echo "$anew"
1 one\n2 two\n3 three

echo something | sed "s/something/$anew/"
1 one
2 two
3 three
The sed 'r' command reads the contents of a file and appends it after the contents of the pattern space.
This is similar to but not the same as what you are trying to do.
Code:
echo something | sed '/something/ r fileA'
something
1 one
2 two
3 three

echo something | sed '/something/ {s/.*//; r fileA
}'
                       # A blank line
1 one
2 two
3 three
 
Old 04-04-2009, 01:45 PM   #6
DeepSeaNautilus
Member
 
Registered: Jul 2008
Posts: 65

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Kenhelm View Post
In bash putting $a in double quotes preserves the newlines
echo "$a"
1 one
2 two
3 three

But the newlines in "$a" will cause sed to fail:
Code:
echo something | sed "s/something/$a/"
sed: -e expression #1, char 17: unterminated `s' command
A way around this is to replace the newlines in "$a" with the string '\n' which GNU sed will output as a newline.
Code:
anew=$(echo "$a" | sed ':a N;s/\n/\\n/;ta')
echo "$anew"
1 one\n2 two\n3 three

echo something | sed "s/something/$anew/"
1 one
2 two
3 three
The sed 'r' command reads the contents of a file and appends it after the contents of the pattern space.
This is similar to but not the same as what you are trying to do.
Code:
echo something | sed '/something/ r fileA'
something
1 one
2 two
3 three

echo something | sed '/something/ {s/.*//; r fileA
}'
                       # A blank line
1 one
2 two
3 three
Thanks, you tought me something new. I was also thinking in replacing the newlines in the original tables with my own delimiters using sed, then use sed to replace the patterns for the contents of the tables and finally replace my own delimiters with the newline characters.

Last edited by DeepSeaNautilus; 04-09-2009 at 09:19 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Reading a CSV text file and storing the values in Oracle Database table shafi2all Linux - Newbie 3 04-17-2008 12:19 PM
Storing part of a filename as a variable VTGuitarMan Programming 5 03-12-2008 06:47 AM
storing output of sed in a variable in shell script Fond_of_Opensource Linux - Newbie 1 11-09-2006 03:57 AM
storing octal value in char variable hubabuba Programming 1 04-17-2006 12:53 AM
mysql and variable table name axelmang Programming 10 09-07-2004 01:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:20 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