Prase a file and store and result to an array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Prase a file and store and result to an array
# 1  
Old 09-08-2008
Prase a file and store and result to an array

Dear all,

I have a file having the following formats:

Code:
ThreadFail=Web1=1234
ThreadFail=Web2=2345
ThreadFail=Web3=12
ConnectionFail=DB1=11
ConnectionFail=DB2=22

The number of lines will be different from every time . How can I parse the file and store the result to an a array inside the shell with the following format :

Code:
ThreadFail[Web1] = 1234
ThreadFail[Web2] = 2345
ThreadFail[Web3] = 12
ConnectionFail[DB1]=11
ConnectionFail[DB2]=22

I now use the following script that can parse out all the separate fields successfully but have not much idea on how to assign the values to an the array with the above format. SmilieSmiliePlease suggest/teach me a method .Thanks so much.

Code:
while read line
do
   echo $line | awk '
BEGIN {FS="="}
{
  if( NF == 3 && $1=="ThreadFail")
  {
         print $1 ":" $2 ":" $3
  }
  else if ( NF == 3 && $1=="ConnectionInUseFail")
  {
        print $1 ":" $2 ":" $3
  }
}
'
done < test.txt

Thanks you very muchSmilieSmilie
# 2  
Old 09-08-2008
cat test.txt
ThreadFail=Web1= 1234
ThreadFail=Web2= 2345
ThreadFail=Web3= 12
ConnectionFail=DB1=11
ConnectionFail=DB2=22

while read line
do
echo $line | awk 'BEGIN {FS="="}
{
if( NF == 3 && $1=="ThreadFail")
{
Thread[NR]=$1;
Web[NR]=$2;
Cnt[NR]=$3;
}
else if ( NF == 3 && $1=="ConnectionFail")
{
Conn[NR]=$1;
DB[NR]=$2;
ConnCnt[NR]=$3;
}
}
{
for (i=1;i<=NR;i++)
{
print Thread[i],Web[i],Cnt[i];
print Conn[i],DB[i],ConnCnt[i];
}
}'
done < test.txt
# 3  
Old 09-08-2008
The while loop is quite superfluous; awk loops over the lines by itself just fine.

In the END clause of the awk script, print out whatever you want the shell to receive, and run that inside eval
# 4  
Old 09-08-2008
Quote:
Originally Posted by era
The while loop is quite superfluous; awk loops over the lines by itself just fine.

In the END clause of the awk script, print out whatever you want the shell to receive, and run that inside eval

Yes , your information is very useful . eval is what exactly I want . Thanks
# 5  
Old 09-09-2008
Quote:
Originally Posted by era
The while loop is quite superfluous; awk loops over the lines by itself just fine.

In the END clause of the awk script, print out whatever you want the shell to receive, and run that inside eval
Hi , I use the following code to prase and assign the value to the array
Code:
cmd=`awk '
BEGIN {FS="="}
{
        if(NF == 3 && $1=="ThreadFail")
        {
                print $1"["$2"]="$3
        }
        else if ( NF ==3 && $1=="ConnectionInUseFail")
        {
                print $1"["$2"]="$3
        }
       
}
' file`
eval $cmd

I check the $cmd and it is assing to the value succesully "ThreadFail[Web1]=1234 ThreadFail[Web2]=2345 ThreadFail[Web3]=12 ConnectionFail[DB1]=11 ConnectionFail[DB2]=22" .However I check that the value of ThreadFail[Web1] , ThreadFail[Web2],ThreadFail[Web3] are also 12 . I echo ThreadFail and it has the value 12 too . I try change to print $1"["$2"]="$3"\n" but it cannot output the newline characters as expected???
# 6  
Old 09-09-2008
Sorry, I missed the requirement to use text subscripts in the array. Which shell are you using? It might not allow textual subscripts for arrays in the first place; you might need to convert them to numbers, or come up with some other workaround. (Maybe name the variables with an underscore, like ConnectionFail_DB1, ConnectionFail_DB2)
# 7  
Old 09-09-2008
Quote:
Originally Posted by era
Sorry, I missed the requirement to use text subscripts in the array. Which shell are you using? It might not allow textual subscripts for arrays in the first place; you might need to convert them to numbers, or come up with some other workaround. (Maybe name the variables with an underscore, like ConnectionFail_DB1, ConnectionFail_DB2)
Hi , I am using the following shell version


Code:
$ bash -version
GNU bash, version 3.00.15(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2004 Free Software Foundation, Inc.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Hit multiple URL from a text file and store result in other test file

Hi, I have a problem where i have to hit multiple URL that are stored in a text file (input.txt) and save their output in different text file (output.txt) somewhat like : cat input.txt http://192.168.21.20:8080/PPUPS/international?NUmber=917875446856... (3 Replies)
Discussion started by: mukulverma2408
3 Replies

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

3. Shell Programming and Scripting

Assign zero to strings that don't appear in block, store result in AWK array

Hi to all, I have this input: <group> <x "2">Group D</x> <x "3">Group B</x> <x "1">Group A</x> </group> <group> <x "1">Group E</x> <x "0">Group B</x> <x "1">Group C</x> </group> <group> ... (11 Replies)
Discussion started by: Ophiuchus
11 Replies

4. Shell Programming and Scripting

Search file for string and store last result to variable

Hi, I'm trying to search a text file for a string: "energy(sigma->0)=". To do so, I executed the grep command, which returned many matches, for example: energy without entropy = -112.16486170 energy(sigma->0) = -112.16520778 energy without entropy = -112.16488936 ... (5 Replies)
Discussion started by: gwr
5 Replies

5. Shell Programming and Scripting

Find diff bet 2 files and store result in another file

Hi I want to compare 2 files. The files have the same amount of rows and columns. So each line must be compare against the other and if one differs from the other, the result of both must be stored in a seperate file. I am doing this in awk. Here is my file1: Blocks... (2 Replies)
Discussion started by: ladyAnne
2 Replies

6. Shell Programming and Scripting

NAWK array to store lines from huge file

Hi, I would like to clarify about the NAWK array to store multiple lines from huge file. The file is having an unique REF.NO, I wants to store the lines (it may be 100+ lines) till I found the new REF.NO. How can I apply NAWK - arrays for the above? Rgds, sharif. (1 Reply)
Discussion started by: sharif
1 Replies

7. Shell Programming and Scripting

Most reliable way to store file contents in an array in bash

Hi Guys, I have a file which has numbers in it separated by newlines as follows: 1.113 1.456 0.556 0.021 -0.541 -0.444 I am using the following code to store these in an array in bash: FILE14=data.txt ARRAY14=(`awk '{print}' $FILE14`) (6 Replies)
Discussion started by: npatwardhan
6 Replies

8. Shell Programming and Scripting

Prase a file and stored the result to an array

Dear all , I have a file whose content has the following format: jboss.web:type=ThreadPool,name=AAAA jboss.web:type=ThreadPool,name=BBBB How can I parse this file to get the value of the name of each line (AAAA , BBBB) and store the result to an array ? (array = AAAA , array = BBBB).... (4 Replies)
Discussion started by: youareapkman
4 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. Shell Programming and Scripting

How to store query multiple result in shell script variable(Array)

:) Suppose,I have one table A. Table A have one column. Table A have 10 rows. I want this 10 rows store into shell script variable. like #!/bin/ksh v_shell_var=Hi here in call oracle , through loop How can I store table A's 10 rows into v_shell_var (Shell Script Array). Regards, Div (4 Replies)
Discussion started by: div_Neev
4 Replies
Login or Register to Ask a Question