passing line into array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting passing line into array
# 1  
Old 07-20-2009
passing line into array

I am doing a shell script in ksh. I have an output from grep that goes something like this:

wordIWasLookingFor
anotherWordIWasLookingFor
yetAnotherWordIWasLookingFor

I want to toss each line into an array such that:

myArray[0] = wordIWasLookingFor
myArray[1] = anotherWordIWasLookingFor
myArray[2] = yetAnotherWordIWasLookingFor

Can anyone tell me how to do this? I was thinking along the lines of:

Code:
myArray[]=`grep LookingFor myFile.txt`

This didn't work though.

Thanks for all answers in advance.
# 2  
Old 07-20-2009
and input ? Example ?
only word/line or ?

If one word / line then
Code:
a=( $(grep -i "string" words.txt ) )
echo ${#a[*]}
echo ${a[*]}

# 3  
Old 07-20-2009
this has been answered by vgersh99
# 4  
Old 07-21-2009
set -A array $(grep "string" myFile.txt )
and
array=( $(grep "string" myFile.txt ) )
can't handle those lines correct which include more words as one.
If you need put the line to the one array element, then you need some input manipulation = line is one element.

Example, works with posix-sh (ksh, bash, ...):
Code:
i=0
array[0]=""

toarr()
{
   array[$i]="$*"
   ((i+=1))
}

grep "string" myFile.txt | while read line
do
     toarr $line
done
echo ${#array[*]}
echo ${array[*]}

i=0
cnt=${#array[*]}
while ((i<cnt))
do
        echo "$i:${array[$i]}"
        ((i+=1))
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Passing parameters and accepting in Array

I am trying to pass parameters to a script which will accept them in array. First parameter is where_clause second parameter is "SRC_TYPE='SYBASE' and PROCESS_CD='BRK'" } echo $2 $ ./abcd.ksh where_clause "SRC_TYPE='SYBASE' and PROCESS_CD='BRK'" + set -A arg_list -- where_clause... (3 Replies)
Discussion started by: ratheeshjulk
3 Replies

2. UNIX for Dummies Questions & Answers

Problems passing strings within an array

i have a list of apps that i need to forcequit and, from time to time, that list changes. perfect excuse to manage a single array! however, my strings with spaces aren't passing as i'd like them to. here's the simple script: #!/bin/sh #-----Array apps=( firefox-bin firefox... (6 Replies)
Discussion started by: hungryd
6 Replies

3. Shell Programming and Scripting

Bash Passing An Array

Good grief so this should be easy. Passing an array as an argument to a function. Here is the sample code: #/bin/bash function foo { local p1=${1} local p2=(${2}) local p3=${3} echo p1 is $p1 echo p2 is $p2 echo p3 is $p3 } d1=data1 d2=data2 a=(bat bar baz) (2 Replies)
Discussion started by: brsett
2 Replies

4. Shell Programming and Scripting

Passing Array to awk

I'm trying to use the following command: awk -v array1=${array1} -f "filename.awk" input.txt Then within filename.awk I want to access array1. However, awk mistakes array1 (the third element of the array) for the input file. How I can pass awk this array? It also appears that awk scripts... (3 Replies)
Discussion started by: B-Ry
3 Replies

5. Shell Programming and Scripting

Passing Shell array to SQLPlus

Dears, Newbie here and tried to search this topic for 3 days now with results. I have a shell array and I want to use it in sqlplus with one connection. here is what I have for now #!/bin/ksh FileName=1000 FileName=2000 FileName=3000 FileName=4000 sqlplus /nolog <<EOF connect... (20 Replies)
Discussion started by: roby2411
20 Replies

6. Shell Programming and Scripting

Passing array variable in shell_exec

Hi all, i wrote a php script in which i passed some values in the array variable using a for loop. I have to pass this array values to a shell script using shell_exec() <?php while($row = mysql_fetch_assoc($ansid)) { //$row = mysql_fetch_assoc($ansid); $aid = $row; echo $aid; $i =... (2 Replies)
Discussion started by: vidhyaS
2 Replies

7. Programming

Passing two dimensional array to a function

Hi. I have a problem with passing two dimensional array to a function. First, let me show my code to explain what i am going to do: I have function:void initialize_board(char board);which is supposed to modify content of passed array. I have read here: Question 6.18 how such arrays should be... (3 Replies)
Discussion started by: Shang
3 Replies

8. Shell Programming and Scripting

[bash] passing array to function

Hi, I'm trying to write a function that reassigns an array to another local array but the method used in reassigning the array reformats the contents of the array which is what I am trying to prevent. The method used to load a file into an array works as expected and the entire array is... (4 Replies)
Discussion started by: ASGR
4 Replies

9. Programming

Passing by value a char array

Hi I am passing or want to pass value of a char array, so that even thoug the called routine is changing the values the calling function should not see the values changed, meaning only copy should be passed Here is the program #include<iostream.h> #include<string.h> void f(char a); int... (5 Replies)
Discussion started by: rkraj
5 Replies

10. Shell Programming and Scripting

[BASH - KSH] Passing array to a function

Passing a array to a function, a basic feature in modern language, seems to be only possible in KSH. Not in BASH. Depite all my efforts I couldn't come to a solution. See the following examples: It works perfectly in KSH: #!/usr/bin/ksh function print_array { # assign array by indirect... (3 Replies)
Discussion started by: ripat
3 Replies
Login or Register to Ask a Question