The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl Function Array Arguement Passing Raynon Shell Programming and Scripting 5 03-27-2009 01:29 PM
Passing a file handler and an array from Perl to Shell Script pinkgladiator Shell Programming and Scripting 5 11-17-2008 11:00 PM
Passing by value a char array rkraj High Level Programming 5 08-12-2008 10:30 AM
[BASH - KSH] Passing array to a function ripat Shell Programming and Scripting 3 04-17-2008 08:17 AM
Help in passing array of inputs to C program using script? ahjiefreak Shell Programming and Scripting 1 03-20-2008 07:36 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-20-2009
mrwatkin mrwatkin is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 37
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 (permalink)  
Old 07-20-2009
kshji's Avatar
kshji kshji is offline
Registered User
  
 

Join Date: Jun 2009
Location: Finland
Posts: 236
and input ? Example ?
only word/line or ?

If one word / line then
Code:
a=( $(grep -i "string" words.txt ) )
echo ${#a[*]}
echo ${a[*]}
  #3 (permalink)  
Old 07-20-2009
ryandegreat25 ryandegreat25 is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 236
this has been answered by vgersh99
  #4 (permalink)  
Old 07-21-2009
kshji's Avatar
kshji kshji is offline
Registered User
  
 

Join Date: Jun 2009
Location: Finland
Posts: 236
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
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:30 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0