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?
# 8  
Old 07-20-2012
Ok. You have series of loops over loops which might be inefficient.
From the post #3, I came to know that there is one more array.

We might help you in a better way if you could tell us the actual requirement with examples.
I guessed something like.. You want to search in a file for records from couple of files including the word "Task started".

Please give us the sample data from all the files and final output you require.
Also, The O/S and Shell version you are using.
# 9  
Old 07-20-2012
I have a file which is seperated by pipe "|" operator
and I want to fetch those lines from the file which has the
the keyword "task started" and the value which i have stored in the array
A line from the file
Code:
2012-05-09 10:29:14.285 | SYNC  | 00050 | DEBUG   | Customer_Rece | Exchange        | Task started                          | B678C56D-96DA-4FFC-B40E-9A032A2EB12E-0000003  | xengine:CERR to ECR CF              | {previous-component=Validation,operties={CSystemID=MS, BSystemID=A1F8D828-15, RId=IUxBNzKlZfghcMxf101209090000024_1, ParserSchema=/apphema_CERR.ecs, IsCorrection=false},}

The value in my array comp is
Code:
xengine:CERR to ECR CF

since this line has 'Task started' and value in array
the output required is
Code:
10:29:14.285  xengine:CERR to ECR CF Customer_Rece

# 10  
Old 07-20-2012
Won't this work for you?

# Create a file containing the patterns. (I think you already having that)
Code:
for var1 in "${comp[@]}"
do
 echo $var1
done > pattern.list


Code:
grep -w "Task started" Sri1.log | grep -f pattern.list



data

Code:
$ cat pattern.list
xengine:CERR to ECR CF
xengine:CERR to ECR FF
xengine:CERR to ECR RF


Code:
$ cat Sri1.log
2012-05-09 10:29:14.285 | SYNC  | 00050 | DEBUG   | Customer_Rece | Exchange        | Task started                          | B678C56D-96DA-4FFC-B40E-9A032A2EB12E-0000003  | xengine:CERR to ECR CF              | {previous-component=Validation,operties={CSystemID=MS, BSystemID=A1F8D828-15, RId=IUxBNzKlZfghcMxf101209090000024_1, ParserSchema=/apphema_CERR.ecs, IsCorrection=false},}
2012-05-09 10:29:14.285 | SYNC  | 00050 | DEBUG   | Customer_Rece | Exchange        | Task going on                          | B678C56D-96DA-4FFC-B40E-9A032A2EB12E-0000003  | xengine:CERR to ECR CF              | {previous-component=Validation,operties={CSystemID=MS, BSystemID=A1F8D828-15, RId=IUxBNzKlZfghcMxf101209090000024_1, ParserSchema=/apphema_CERR.ecs, IsCorrection=false},}
2012-05-09 10:29:14.285 | SYNC  | 00050 | DEBUG   | Customer_Rece | Exchange        | Task started                          | B678C56D-96DA-4FFC-B40E-9A032A2EB12E-0000003  | XXXXXXX:CERR to ECR CF              | {previous-component=Validation,operties={CSystemID=MS, BSystemID=A1F8D828-15, RId=IUxBNzKlZfghcMxf101209090000024_1, ParserSchema=/apphema_CERR.ecs, IsCorrection=false},}
2012-05-09 10:29:14.285 | SYNC  | 00050 | DEBUG   | Customer_Rece | Exchange        | Task ended                          | B678C56D-96DA-4FFC-B40E-9A032A2EB12E-0000003  | XXXXXXX:CERR to ECR CF              | {previous-component=Validation,operties={CSystemID=MS, BSystemID=A1F8D828-15, RId=IUxBNzKlZfghcMxf101209090000024_1, ParserSchema=/apphema_CERR.ecs, IsCorrection=false},}

# 11  
Old 07-20-2012
Given clx's files sri1.log and pattern.list, would this be an option for you:

Code:
while read var; do grep -Ei "task started.*$var" sri1.log | cut -d"|" -f1,5,9; done <pattern.list

resulting in

Code:
2012-05-09 10:29:14.285 | Customer_Rece | xengine:CERR to ECR CF


Last edited by RudiC; 07-20-2012 at 09:32 AM.. Reason: Adding result line
# 12  
Old 07-25-2012
I tried the option given but its not taking the keyword "Task Started".
Can you give another alternate for this?
# 13  
Old 07-25-2012
Are you sure to grep the exact case (upper/lower)?

I can see it varies with "Tast Started", "task started" or "Tast started". Just check and use the correct string to match.

If you are not sure about the case and want to do a case-independent search,
use "grep -i".

Code:
grep -iw "Task started" Sri1.log | grep -f pattern.list

also try

Code:
grep -i "Task started" Sri1.log | grep -f pattern.list

# 14  
Old 07-25-2012
I have tried all the options but its not working.
The file pattern.list has:
Code:
B678C56D-96DA-4FFC-B40E-9A032A2EB12E| xengine:CERR to ECR CF
B678C56D-96DA-4FFC-B40E-9A032A2EB12E| service.adapter:UCF Version Creator
4ECF997A-BA1A-4497-B48F-6CA27414567A| service.adapter:UCF Writer_2

The file Sri1.log has:
Code:
2012-05-09 10:29:14.284 | SYNC  | 00041 | DEBUG   | Customer_Receive Payment | Task            | Task started on a component            | B678C56D-96DA-4FFC-B40E-9A032A2EB12E          | xengine:CERR to ECR CF              | {}
2012-05-09 10:29:14.285 | SYNC  | 00050 | DEBUG   | Customer_Receive Payment | Exchange        | Exchange sent                          | B678C56D-96DA-4FFC-B40E-9A032A2EB12E-0000003  | xengine:CERR to ECR CF              | {previous-component=Validation,{ClaimS RemittanceId=IUxBNzKlZfghcMxf101209090000024_1}
2012-05-09 10:29:14.285 | SYNC  | 00051 | DEBUG   | Customer_Receive Payment | Exchange        | Exchange processing started            | B678C56D-96DA-4FFC-B40E-9A032A2EB12E-0000003  | xengine:CERR to ECR CF              | {previous-component=Validation, Split, exchange-properties={ClaimSystemID= RemittanceId=IUxBNzKlZfghcMxf101209090000024_1}
2012-05-09 10:29:28.677 | SYNC  | 00055 | DEBUG   | Customer_Receive Payment | Exchange        | Exchange created                       | B678C56D-96DA-4FFC-B40E-9A032A2EB12E-0000004  | xengine:CERR to ECR CF              | {previous-component=Validation, Split, exchange-properties={BAMSystem, RemittanceId=IUxBNzKlZfghcMxf101209090000024_1}, output-name=Split Good}
2012-05-09 10:29:28.679 | SYNC  | 00041 | DEBUG   | Customer_Receive Payment | Task            | Task started on a component            | B678C56D-96DA-4FFC-B40E-9A032A2EB12E          | service.adapter:UCF Version Creator | {}
2012-05-09 10:29:28.680 | SYNC  | 00050 | DEBUG   | Customer_Receive Payment | Exchange        | Exchange sent                          | B678C56D-96DA-4FFC-B40E-9A032A2EB12E-0000004  | service.adapter:UCF Version Creator | {previous RemittanceId=IUxBNzKlZfghcMxf101209090000024_1}
2012-05-09 10:29:28.680 | SYNC  | 00051 | DEBUG   | Customer_Receive Payment | Exchange        | Task started                           | B678C56D-96DA-4FFC-B40E-9A032A2EB12E-0000004  | service.adapter:UCF Version Creator | {previous-component=CERR to ECR CF, exchange-properties={BAMSystemID=A1F8D828-15D0-4DE2-8B6E-672BD8, RemittanceId=IUxBNzKlZfghcMxf101209090000024_1}
2012-05-09 10:32:05.439 | SYNC  | 00040 | INFO    | EUO_Payment Validation   | Task            | Task started                           | C3EDAE4F-2F73-402F-AABE-1C3B9FEC4314          | subroute.endpoint:Payment Split Completion | {}
2012-05-09 10:32:05.451 | SYNC  | 00050 | DEBUG   | EUO_Payment Validation   | Exchange        | Exchange sent                          | 4ECF997A-BA1A-4497-B48F-6CA27414567A-0000006  | service.adapter:UCF Writer_2        | {}

I want lines having the word "Task started" and the value stored in the file pattern.list
I tried the following code :
Code:
while read var
	do 
		grep -iw "Task started" Sri1.log | grep -f pattern.list | cut -d"|" -f1,5,9 
	done <pattern.list

the output its giving is
Code:
2012-05-09 10:29:14.284 | Customer_Receive Payment | xengine:CERR to ECR CF
2012-05-09 10:29:14.285 | Customer_Receive Payment | xengine:CERR to ECR CF
2012-05-09 10:29:28.677 | Customer_Receive Payment | xengine:CERR to ECR CF
2012-05-09 10:29:28.679 | Customer_Receive Payment | service.adapter:UCF Version Creator
2012-05-09 10:29:28.680 | Customer_Receive Payment | service.adapter:UCF Version Creator

but the output should be:
Code:
2012-05-09 10:29:28.680 | Customer_Receive Payment | service.adapter:UCF Version Creator
2012-05-09 10:32:05.439 | EUO_Payment Validation   |subroute.endpoint:Payment Split Completion

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