For Programmers: Free Programming Magazines  


Home > Archive > AWK > December 2006 > how to store strings in variables with awk, bash?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how to store strings in variables with awk, bash?
Luqman

2006-12-13, 4:08 pm

hello,

I have a file.txt with following content:
---------------------------file.txt------------------------------
109
69
47
46
53
49
79
83
--------------------------------------------------------------------

----------------------------myscript.sh-------------------------
#!/bin/bash

X=0
while [ $X -le $rlc_blocks ]
#while [ $X -lt 3 ]
do
X=$((X+1))
{myvariable[$X]} = $(mawk '{print $3}' file.txt)
done
------------------------------------------------------------------------

Can anybody help me with this script? How do I store individual numbers
from different lines of file.txt into different variables?

Regards,

--
Luqman

Chris F.A. Johnson

2006-12-13, 4:08 pm

On 2006-12-12, Luqman wrote:
> hello,


The belongs in comp.unix.shell, not comp.lang.awk; cross-posted and
follow-up set.

> I have a file.txt with following content:
> ---------------------------file.txt------------------------------
> 109
> 69
> 47
> 46
> 53
> 49
> 79
> 83
> --------------------------------------------------------------------
>
> ----------------------------myscript.sh-------------------------
> #!/bin/bash
>
> X=0
> while [ $X -le $rlc_blocks ]
> #while [ $X -lt 3 ]
> do
> X=$((X+1))
> {myvariable[$X]} = $(mawk '{print $3}' file.txt)
> done
> ------------------------------------------------------------------------
>
> Can anybody help me with this script? How do I store individual numbers
> from different lines of file.txt into different variables?


eval "$(
awk '{ printf "var_%s=%s\n", NR, $1 }' file.txt
)"


Or:

X=1
while read num
do
eval "var_$X=\$num"
done < file.txt


Or use a bash array:

var=( $(< file.txt) )



--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com