|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
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]}"
donecontent 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.
|
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
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
|
||||
|
||||
|
echo it without double quotes Code:
echo ${a[i]} |
|
#4
|
|||
|
|||
|
Also try line: Code:
a[$i]=$(awk '{for (i=1; i<=NF; i++) print $(i)}' /home/rama/expenese.txt) |
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
my actual goal is i want to recall array values again some whereelse in script
|
| Sponsored Links | |
|
|
#6
|
||||
|
||||
|
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
|
|||
|
|||
|
Quote:
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|