Sponsored Content
Top Forums Shell Programming and Scripting How to read values and store in array? Post 302671313 by bakunin on Friday 13th of July 2012 10:47:11 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

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

8. 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

9. 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

10. 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
JudySL(3)						     Library Functions Manual							 JudySL(3)

NAME
JudySL macros - C library for creating and accessing a dynamic array, using a null-terminated string as an Index (associative array) SYNOPSIS
cc [flags] sourcefiles -lJudy #include <Judy.h> #define MAXLINELEN 1000000 // define maximum string length Word_t * PValue; // JudySL array element uint8_t Index[MAXLINELEN]; // string int Rc_int; // return value Word_t Rc_word; // full word return value Pvoid_t PJSLArray = (Pvoid_t) NULL; // initialize JudySL array JSLI( PValue, PJSLArray, Index); // JudySLIns() JSLD( Rc_int, PJSLArray, Index); // JudySLDel() JSLG( PValue, PJSLArray, Index); // JudySLGet() JSLFA(Rc_word, PJSLArray); // JudySLFreeArray() JSLF( PValue, PJSLArray, Index); // JudySLFirst() JSLN( PValue, PJSLArray, Index); // JudySLNext() JSLL( PValue, PJSLArray, Index); // JudySLLast() JSLP( PValue, PJSLArray, Index); // JudySLPrev() DESCRIPTION
A JudySL array is the equivalent of a sorted set of strings, each associated with a Value (word). A Value is addressed by an Index (key), which is a null-terminated character string of any length. Memory to support the array is allocated as index/value pairs are inserted, and released as index/value pairs are deleted. This is a form of associative array, where array elements are also sorted lexicographically (case-sensitive) by indexes. This could be thought of as void * JudySLArray["Toto, I don't think we're in Kansas any more"]; A JudySL array is allocated with a NULL pointer Pvoid_t PJSLArray = (Pvoid_t) NULL; As with an ordinary array, there are no duplicate indexes (strings) in a JudySL array. Using the macros described here, rather than the JudySL function calls, the default error handling sends a message to the standard error and terminates the program with exit(1). JSLI(PValue, PJSLArray, Index) // JudySLIns() Insert an Index string and Value in the JudySL array PJSLArray. If the Index is successfully inserted, the Value is initialized to 0. If the Index was already present, the Value is not modified. Return PValue pointing to Index's Value. Your program must use this pointer to modify the Value, for example: *PValue = 1234; Note: JSLI() and JSLD reorganize the JudySL array. Therefore, pointers returned from previous JudySL calls become invalid and must be reacquired. JSLD(Rc_int, PJSLArray, Index) // JudySLDel() Delete the specified Index/Value pair (array element) from the JudySL array. Return Rc_int set to 1 if successful. array and it was previously inserted. Return Rc_int set to 0 if Index was not present. JSLG(PValue, PJSLArray, Index) // JudySLGet() Get the pointer to Index's Value. Return PValue pointing to Index's Value. Return PValue set to NULL if the Index was not present. JSLFA(Rc_word, PJSLArray) // JudySLFreeArray() Given a pointer to a JudySL array (PJSLArray), free the entire array (much faster than using a JSLN(), JSLD() loop.) Return Rc_word set to the number of bytes freed and PJSLArray set to NULL. JudySL Search Functions The JudySL search functions allow you to search for indexes in the array. You may search inclusively or exclusively, in either forward or reverse directions. If successful, Index is returned set to the found index, and PValue is returned set to a pointer to Index's Value. If unsuccessful, PValue is returned set to NULL, and Index contains no useful information. PValue must be tested for non-NULL prior to using Index, since a search failure is possible. Note: To accomodate all possible returns, the Index buffer must be at least as large as the largest string stored in the array. JSLF(PValue, PJSLArray, Index) // JudySLFirst() Search (inclusive) for the first index present that is equal to or greater than the passed Index string. (Start with a null string to find the first index in the array.) JSLF() is typically used to begin a sorted-order scan of the valid indexes in a JudySL array. uint8_t Index[MAXLINELEN]; strcpy (Index, ""); JSLF(PValue, PJSLArray, Index); JSLN(PValue, PJSLArray, Index) // JudySLNext() Search (exclusive) for the next index present that is greater than the passed Index string. JSLN() is typically used to continue a sorted- order scan of the valid indexes in a JudySL array, or to locate a "neighbor" of a given index. JSLL(PValue, PJSLArray, Index) // JudySLLast() Search (inclusive) for the last index present that is equal to or less than the passed Index string. (Start with a maximum-valued string to look up the last index in the array, such as a max-length string of 0xff bytes.) JSLL() is typically used to begin a reverse-sorted- order scan of the valid indexes in a JudySL array. JSLP(PValue, PJSLArray, Index) // JudySLPrev() Search (exclusive) for the previous index present that is less than the passed Index string. JSLP() is typically used to continue a reverse-sorted-order scan of the valid indexes in a JudySL array, or to locate a "neighbor" of a given index. ERRORS
: See: Judy_3.htm#ERRORS EXAMPLE of a string sort routine #include <stdio.h> #include <Judy.h> #define MAXLINE 1000000 // max string (line) length uint8_t Index[MAXLINE]; // string to insert int // Usage: JudySort < file_to_sort main() { Pvoid_t PJArray = (PWord_t)NULL; // Judy array. PWord_t PValue; // Judy array element. Word_t Bytes; // size of JudySL array. while (fgets(Index, MAXLINE, stdin) != (char *)NULL) { JSLI(PValue, PJArray, Index); // store string into array if (PValue == PJERR) // if out of memory? { // so do something printf("Malloc failed -- get more ram "); exit(1); } ++(*PValue); // count instances of string } Index[0] = ''; // start with smallest string. JSLF(PValue, PJArray, Index); // get first string while (PValue != NULL) { while ((*PValue)--) // print duplicates printf("%s", Index); JSLN(PValue, PJArray, Index); // get next string } JSLFA(Bytes, PJArray); // free array fprintf(stderr, "The JudySL array used %lu bytes of memory ", Bytes); return (0); } AUTHOR
Judy was invented by Doug Baskins and implemented by Hewlett-Packard. SEE ALSO
Judy(3), Judy1(3), JudyL(3), JudyHS(3), malloc(), the Judy website, http://judy.sourceforge.net, for further information and Application Notes. JudySL(3)
All times are GMT -4. The time now is 05:55 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy