hi
My script as below
#!/bin/ksh
for i in `seq 1 7`
do
a[$i]=$(awk '{print $i}' /home/rama/expenese.txt)
done
for i in `seq 1 7`
do
echo "${a}"
done
content of expense.txt is as below
5032 210179 3110 132813874 53488966 11459221 5300794
I want output as below
5032
210179
3110
132813874
53488966
11459221
5300794
but after executing above script, output as below
5032 210179 3110 132813874 53488966 11459221 5300794
5032 210179 3110 132813874 53488966 11459221 5300794
5032 210179 3110 132813874 53488966 11459221 5300794
5032 210179 3110 132813874 53488966 11459221 5300794
5032 210179 3110 132813874 53488966 11459221 5300794
5032 210179 3110 132813874 53488966 11459221 5300794
5032 210179 3110 132813874 53488966 11459221 5300794
can anyone help me to get desired output, Thanks in advance.
There's almost never a reason to dump a file into an array in shell, let alone a silly method that runs awk on the same line 7 times. I've seen people who think they have to run awk n times for n lines before, but n*7 times for n lines is a new one on me!
This accomplishes the output you wanted without awk or arrays in one line:
$ tr -s ' \t' '\n' < /home/rama/expenese.txt
5032
210179
3110
132813874
53488966
11459221
5300794
$
If you really do want it stored in your shell, you could do this:
# works in any bourne shell
set -- `cat file`
echo $1
echo $2
or
# requires ksh
set -A arrname `cat file`
But again, there's almost never any reason to dump a file into an array in shell. Please explain your actual goal.
echo it without double quotes
echo ${a}
rdrtx1
October 23, 2012, 11:39am
4
Also try line:
a[$i]=$(awk '{for (i=1; i<=NF; i++) print $(i)}' /home/rama/expenese.txt)
my actual goal is i want to recall array values again some whereelse in script
$ cat e.txt
5032 210179 3110 132813874 53488966 11459221 5300794
$ arr=(`cat e.txt`)
$ echo ${arr[0]}
5032
$ echo ${arr[1]}
210179
$ echo ${arr[2]}
3110
$ echo ${arr[4]}
53488966
$ echo ${arr[3]}
132813874
$ echo ${arr[5]}
11459221
$ echo ${arr[6]}
5300794
That's not your goal, that's the way you've chosen to solve the problem.
What do you want to recall these values for? Why an array and not some other kind of variable? To recall them in order? That's what a shell for loop is for.