Assigning array values using awk in shell scripting | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Assigning array values using awk in shell scripting

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-23-2012
Ramakrishna V Ramakrishna V is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 23 October 2012, 11:50 AM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Data Assigning array values using awk in shell scripting

hi
My script as below


Code:
#!/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[i]}"
done

content of expense.txt is as below

Code:
5032     210179         3110     132813874   53488966   11459221    5300794

I want output as below

Code:
5032
210179
3110
132813874
53488966
11459221
5300794

but after executing above script, output as below

Code:
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.

Moderator's Comments:
Please use code tags next time for your code and data.
Sponsored Links
    #2  
Old 10-23-2012
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 22 September 2014, 6:10 PM EDT
Location: Saskatchewan
Posts: 19,406
Thanks: 790
Thanked 3,273 Times in 3,068 Posts
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:

Code:
$ 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:


Code:
# works in any bourne shell
set -- `cat file`

echo $1
echo $2

or
Code:
# 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.

Last edited by Corona688; 10-23-2012 at 11:38 AM..
Sponsored Links
    #3  
Old 10-23-2012
itkamaraj's Avatar
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 8 August 2014, 4:56 AM EDT
Posts: 3,057
Thanks: 33
Thanked 658 Times in 637 Posts
echo it without double quotes


Code:
echo ${a[i]}

    #4  
Old 10-23-2012
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 15 July 2014, 11:14 AM EDT
Location: Houston, Texas, USA
Posts: 675
Thanks: 0
Thanked 203 Times in 195 Posts
Also try line:

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

Sponsored Links
    #5  
Old 10-23-2012
Ramakrishna V Ramakrishna V is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 23 October 2012, 11:50 AM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
my actual goal is i want to recall array values again some whereelse in script
Sponsored Links
    #6  
Old 10-23-2012
itkamaraj's Avatar
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 8 August 2014, 4:56 AM EDT
Posts: 3,057
Thanks: 33
Thanked 658 Times in 637 Posts

Code:
$ 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

Sponsored Links
    #7  
Old 10-23-2012
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 22 September 2014, 6:10 PM EDT
Location: Saskatchewan
Posts: 19,406
Thanks: 790
Thanked 3,273 Times in 3,068 Posts
Quote:
Originally Posted by Ramakrishna V View Post
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.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Assigning values for a dynamic array for an input suneelj Shell Programming and Scripting 2 12-10-2009 01:02 PM
Assigning values to an array via for/while loop fiori_musicali Shell Programming and Scripting 2 11-24-2008 10:01 PM
Assigning the values to an Array kkraja Shell Programming and Scripting 1 08-11-2008 06:28 AM
perl: Assigning array values.. looza Shell Programming and Scripting 4 07-15-2008 08:08 AM
Assigning values to an array yongho UNIX for Dummies Questions & Answers 4 07-13-2005 08:49 PM



All times are GMT -4. The time now is 06:18 PM.