How to read values and store in array?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read values and store in array?
# 1  
Old 07-13-2012
How to read values and store in array?

I am reading a value from a file and want to store the value in a dynamic array as i don't know the number of occurrences of the value in that file.
How can i do that and then later fetch that value from array
# 2  
Old 07-13-2012
You do it straightforward. An array is just another variable after all and variables in shell scripts can be created just by using them.

Alas you haven't said which shell you are using, so as your penance for omitting this vital information i just suppose you are using Korn shell (ksh) and if you don't you will have to translate my solution to your shell yourself. ;-))

Suppose the following input file. We want all the lines "data=<value>" and store the "<value>" in an indexed array (1-based):

Code:
# this is a test file
lalala
data=first array element
some more meaningless text
data=second array element
data=third array element
# we do not want this line to do anything
data=last array element
foo bar

Now the script, which will read it and put the content into an array, like stated above:

Code:
#! /bin/ksh
# first some declarations
typeset -i Index=0
typeset    Line=""
typeset    Input="/path/to/file.above"

# At first we fill the array:
(( Index = 1 ))                               # we want to start our indices with 1
grep "^data=" "$Input" | while read Line ; do
     typeset Array[$Index]="${Line##data=}"   # split off "data=", leaving value
     (( Index += 1 ))                         # increment index
done

# now let us try to display the array contents:
(( Index = 1 ))                               # reset our array index
                                              # ${#Array[*]} evaluates to the number of elements in the array
while [ $Index -le ${#Array[*]} ] ; do
     print - "display array index ${Index}:\t ${Array[$Index]}"
     (( Index += 1 ))
done

exit 0

I hope this helps.

bakunin
# 3  
Old 07-19-2012
Thanku for the reply..It was helpful

I am facing another issues
I have two arrays:
Code:
Taskid
comp

I want to fetch values from file based on values in these arrays. My file name is Sri1.log
I have written this code:
Code:
for var in  "${Taskid[@]}"
do
	for var1 in "${comp[@]}"
	do
		while read line
		do
			if[ grep -w 'Task started' && $var1]
			then
				cut -d"|" -f1 Sri1.log >> time.txt
			fi
		done< Sri1.log	
	done
done

But gives the error
Code:
syntax error near unexpected token 'then'

Please guide me to resolve this
# 4  
Old 07-19-2012
Quote:
Originally Posted by Prachi Gupta
Code:
			if[ grep -w 'Task started' && $var1]
			then
				cut -d"|" -f1 Sri1.log >> time.txt
			fi

But gives the error
Code:
syntax error near unexpected token 'then'

Please guide me to resolve this
There must be a space on both side of "[" and "]"
Inner commands should be first executed, $(..) or `...`
Also, when using && constructs, you should probably use ..
Code:
if [ "$(grep -w 'Task started')" ] && [ $var1 ]
			then
				cut -d"|" -f1 Sri1.log >> time.txt
			fi

# 5  
Old 07-19-2012
Thanku it worked
but
it is giving error
Code:
 [: too many arguments
[: binary operator expected
[: unary operator expected

# 6  
Old 07-20-2012
Can you post the updated version of script/code ?
# 7  
Old 07-20-2012
Thanx for the help..I worked but currently I am stuck in
I want only those lines from the file which have value in array and keyword "Task started"
i have written the following code
Code:
for var1 in "${comp[@]}"
 do
 
  while read line
  do
   if [ " $(echo $line | grep -w 'Task started') " ] && [ " $(echo $line | grep "$var1") " ]
   then
   echo $line >>time1.txt
   fi
  done< Sri1.log 
  echo 'calculating time' >> time1.txt
 done

but its not working as it is giving me all the lines from the file
Please help in this
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

2. UNIX for Dummies Questions & Answers

How to store/read multiple values from a varible

Hi, when I enter 'ps -ef| grep process_name'/'psu | grep process_name', i am getting multiple number of lines output( i mean multiple no of processes).how can i store it one by one and echo it in the same way(one by one). part of script is var1=$(remsh hostname -l username ps -ef|grep... (2 Replies)
Discussion started by: jeanzibbin
2 Replies

3. Shell Programming and Scripting

Store the output values in array

Hi, How to store the values in array from output result, EG: I have the result like this, ps, google, 1.txt, 1 sam, google, 2.txt, 2 These are the four values followed by comma in two sets. I need to store these values set by set. One set contains four values followed by comma. ... (2 Replies)
Discussion started by: KarthikPS
2 Replies

4. Shell Programming and Scripting

Store values from a file into an array variable in Shell

Dear All, I have been trying to do a simple task of extracting 2 fields from the file (3 rows) and store it in an array variable. I tried with: #! /bin/bash ch=`cut -f10 tmp.txt` counter=0 for p in $pid do c=${ch} echo "$c ..$counter" counter=$((counter+1))... (2 Replies)
Discussion started by: ezhil01
2 Replies

5. Shell Programming and Scripting

Not able to read unique values in array

Hi Friends, I am having some trouble reading into an array. Basically, I am trying to grep for a pattern and extract it's value and store the same into an array. For eg., if my input is: <L:RECORD>name=faisel farooq,age=21, company=TCS,project=BT</L:RECORD> <L:RECORD>name=abc... (1 Reply)
Discussion started by: faiz1985
1 Replies

6. Shell Programming and Scripting

Store values in an Array

Hi all. Well, I have the next code: I need to make an array with the values I have in the bucle, but just don't get it... Question is, how can I store in an array that values, and how can I display them with echo? (8 Replies)
Discussion started by: crcbad
8 Replies

7. Shell Programming and Scripting

Read textfile and enter the values in array

Hi, I want to put values in .txt file into array. Example : $vi repo.txt abc def ghi jkl mno pqr i want the output to be like this: $echo ${mydf} abc $echo ${mydf} def $echo ${mydf} ghi (3 Replies)
Discussion started by: luna_soleil
3 Replies

8. UNIX for Dummies Questions & Answers

trouble using read to store values in variables from command output

I know there are caveats about using read in pipelines because read is treated by a subshell. I know this but I can't think of any way to accomplish this regardless, I'm still a rookie. I hope somebody will be able to interpret what it is that I'm trying to accomplish and correct me. ... (2 Replies)
Discussion started by: ProGrammar
2 Replies

9. UNIX for Dummies Questions & Answers

How to store the values in a file into an array

Hi, I do have a file and the contents are as follws: 10 20 30 40 50 Now I want to store those values into an array. How can be done this ?? (3 Replies)
Discussion started by: risshanth
3 Replies

10. UNIX for Advanced & Expert Users

how do I store the values in array using shell

Hi, Is is possible to get the value using shell script? x=1 y1 = 10 y2 = 15 y3 = 7 echo $y$x is giving y1 (variable name) but I need the value of y1 (i.e. 10 dynamically) Is there any solution? if so, please mail me at kkodava@maxis.com.my ... (2 Replies)
Discussion started by: krishna
2 Replies
Login or Register to Ask a Question