# Assigning array values using awk in shell scripting

**URL:** <https://community.unix.com/t/assigning-array-values-using-awk-in-shell-scripting/318704>\
**Category:** Shell Programming and Scripting\
**Created:** [October 23, 2012, 11:27am UTC](https://community.unix.com/t/assigning-array-values-using-awk-in-shell-scripting/318704 "2012-10-23T11:27:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ramakrishna\_V](https://community.unix.com/letter_avatar/ramakrishna_v/32/6_449c533f586939b6e5f246632a46421f.png) [@Ramakrishna\_V](https://community.unix.com/u/Ramakrishna_V)\
**Post date:** [October 23, 2012, 11:27am UTC](https://community.unix.com/t/assigning-array-values-using-awk-in-shell-scripting/318704/1 "2012-10-23T11:27:42Z")

</div>

hi  
My script as below

```nohighlight
#!/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

```nohighlight
5032 210179 3110 132813874 53488966 11459221 5300794 

```

I want output as below

```nohighlight
5032
210179
3110
132813874
53488966
11459221
5300794

```

but after executing above script, output as below

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [October 23, 2012, 11:31am UTC](https://community.unix.com/t/assigning-array-values-using-awk-in-shell-scripting/318704/2 "2012-10-23T11:31:46Z")

</div>

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:

```nohighlight
$ 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:

```nohighlight
# works in any bourne shell
set -- `cat file`

echo $1
echo $2

```

or

```nohighlight
# 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.

---

<div class="post-metadata">

**Author:** ![itkamaraj](https://community.unix.com/user_avatar/community.unix.com/itkamaraj/32/1880_2.png) [@itkamaraj](https://community.unix.com/u/itkamaraj)\
**Post date:** [October 23, 2012, 11:32am UTC](https://community.unix.com/t/assigning-array-values-using-awk-in-shell-scripting/318704/3 "2012-10-23T11:32:11Z")

</div>

echo it without double quotes

```nohighlight
echo ${a}

```

---

<div class="post-metadata">

**Author:** ![rdrtx1](https://community.unix.com/user_avatar/community.unix.com/rdrtx1/32/2721_2.png) [@rdrtx1](https://community.unix.com/u/rdrtx1)\
**Post date:** [October 23, 2012, 11:39am UTC](https://community.unix.com/t/assigning-array-values-using-awk-in-shell-scripting/318704/4 "2012-10-23T11:39:30Z")

</div>

Also try line:

```nohighlight
a[$i]=$(awk '{for (i=1; i<=NF; i++) print $(i)}' /home/rama/expenese.txt)

```

---

<div class="post-metadata">

**Author:** ![Ramakrishna\_V](https://community.unix.com/letter_avatar/ramakrishna_v/32/6_449c533f586939b6e5f246632a46421f.png) [@Ramakrishna\_V](https://community.unix.com/u/Ramakrishna_V)\
**Post date:** [October 23, 2012, 11:50am UTC](https://community.unix.com/t/assigning-array-values-using-awk-in-shell-scripting/318704/5 "2012-10-23T11:50:10Z")

</div>

my actual goal is i want to recall array values again some whereelse in script

---

<div class="post-metadata">

**Author:** ![itkamaraj](https://community.unix.com/user_avatar/community.unix.com/itkamaraj/32/1880_2.png) [@itkamaraj](https://community.unix.com/u/itkamaraj)\
**Post date:** [October 23, 2012, 11:57am UTC](https://community.unix.com/t/assigning-array-values-using-awk-in-shell-scripting/318704/6 "2012-10-23T11:57:20Z")

</div>

```nohighlight
$ 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

```

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [October 23, 2012, 1:02pm UTC](https://community.unix.com/t/assigning-array-values-using-awk-in-shell-scripting/318704/7 "2012-10-23T13:02:31Z")

</div>

> [@ramakrishna v](#):
>
> my actual goal is i want to recall array values again some whereelse in script

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.
