Getting multiple lines of text into a variable?

Getting multiple lines of text into a variable?

Post by Timur Tab » Fri, 17 Jun 2005 07:04:05



I'm using bash on Linux, and I'm trying to create a variable that
contains multiple lines of text.  I want to then pass this variable as
a parameter to a function, which will create a file that contains
(among other things), the contents of this variable.  So I have this:

TMPFILE=/tmp/bla

MYVAR=$(cat <<EOF1
hello
there
EOF1)

myfunc $TMPFILE $MYVAR

function myfunc
{
        ed -s $1 <<EOF2
1i
first line
$2
.
w
q
EOF2

Quote:}

This code is supposed to pre-pend the lines "hello" and "there" to file
/tmp/bla.  The problem is that MYVAR contains the string "hello there"
on one line.

I actually don't care about the variable part, I just want to pass
multiple lines of text from a here document into myfunc.  I just
thought using a variable would be the easiest, but I'm open to
suggestions.  I don't know Perl, so that's not an option for me.

 
 
 

Getting multiple lines of text into a variable?

Post by Chris F.A. Johnso » Fri, 17 Jun 2005 07:16:33



Quote:> I'm using bash on Linux, and I'm trying to create a variable that
> contains multiple lines of text.  I want to then pass this variable as
> a parameter to a function, which will create a file that contains
> (among other things), the contents of this variable.  So I have this:

> TMPFILE=/tmp/bla

> MYVAR=$(cat <<EOF1
> hello
> there
> EOF1)

    Or:

MYVAR='hello
there'

Quote:> myfunc $TMPFILE $MYVAR

myfunc "$TMPFILE" "$MYVAR"

Quote:> function myfunc
> {
>         ed -s $1 <<EOF2
> 1i
> first line
> $2
> .
> w
> q
> EOF2
> }

myfunc()
{
   tempfile=$HOME/whatever.$$
   {
     printf "%s\n" "$2"
     cat "$1"
   } > "$tempfile"
   mv "$tempfile" "$1"

Quote:}
> This code is supposed to pre-pend the lines "hello" and "there" to file
> /tmp/bla.  The problem is that MYVAR contains the string "hello there"
> on one line.

   Quote your variables. See my examples, above.

Quote:> I actually don't care about the variable part, I just want to pass
> multiple lines of text from a here document into myfunc.  I just
> thought using a variable would be the easiest, but I'm open to
> suggestions.  I don't know Perl, so that's not an option for me.

--
    Chris F.A. Johnson                     <http://cfaj.freeshell.org>
    ==================================================================
    Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
    <http://www.torfree.net/~chris/books/cfaj/ssr.html>

 
 
 

Getting multiple lines of text into a variable?

Post by Timur Tab » Fri, 17 Jun 2005 07:36:34



> MYVAR='hello
> there'

That doesn't work:

$ cat > tst
#!/bin/sh
MYVAR='hello
there'
echo $MYVAR

$ ./tst
hello there

 
 
 

Getting multiple lines of text into a variable?

Post by Chris F.A. Johnso » Fri, 17 Jun 2005 07:46:37




>> MYVAR='hello
>> there'

> That doesn't work:

> $ cat > tst
> #!/bin/sh
> MYVAR='hello
> there'
> echo $MYVAR

> $ ./tst
> hello there

   I repeat, quote your variables:

$ echo "$MYVAR"
hello
there

--
    Chris F.A. Johnson                     <http://cfaj.freeshell.org>
    ==================================================================
    Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
    <http://www.torfree.net/~chris/books/cfaj/ssr.html>

 
 
 

1. How to read lines from text file with multiple columns into individual variables

I am new to bash programming, so this is probably a very easy
question.

I have a text file with several lines (rows) each having two columns.
(A simple database with one record per row, the columns separated by
whitespace).

I need to be able to go through the entire file line by line, obtain
the two values from each row, place them in two different variables
and then run a process on the two values.

I have tried using

for name in $(awk {print $1, $2} ) < filetoprocess

do
  echo $name   # for testing
done
But this puts each variable in its own loop. I need to have both $1
and $2 available in the same loop.

What I need is

for <what do I put here?> < filetoprocess
do
   anotherprocess $var1 $var2  
   #where var1 & var 2 represent the two values obtained from
   #each line of filetoprocess
done

I am sure this is trivial, but I can't figure it out.

Keith

2. RedHat 5.0

3. Extracting multiple lines OR deleting multiple lines from a file using AWK

4. Fake Oakley Sunglasses!

5. Assigning lines of text to variables

6. renaming a file

7. grepping for long lines in variable width text

8. Unix restore is very slow

9. How can I store one line of a text file into a shell variable?

10. Assigning lines of text to variables

11. Getting variables from a text file

12. POSIX SHELL : multiple line variable

13. How to #define multiple text lines in Unix c source code