Match Pattern and store next value into array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match Pattern and store next value into array
# 1  
Old 12-15-2011
Match Pattern and store next value into array

Hi,

I am trying to write a script which parses a log file and will eventually put the values in an array so that I can perform some math on it. In this file I am only interested in the last 200 lines so here is the command I use to display the contents in a manageable manner.
Code:
tail -200 /home/log.txt | grep -A6 'Header' | grep -v 'Header'

The output is like this
Code:
 
Some numbers and characters 1 = 5.09093e-03 C
Some numbers and characters 2 = 6.09030e-03 C
Some numbers and characters 3 = 4.48998e-03 C
Some numbers and characters 4 = 4.55334e-03 C
Some numbers and characters 5 = 7.38398e-02 C

I would like to store the numbers after the = sign into an array cutting off the whitespace in front and the whitespace, C and newline character at the end.

Thanks

Last edited by vbe; 12-15-2011 at 05:32 AM.. Reason: use code tags for your data and code next time
# 2  
Old 12-15-2011
Try this...

Code:
#If your set supports -A option
set -A arr $(tail -200 /home/log.txt | grep -A6 'Header' | grep -v 'Header' | awk '{printf $(NF-1)" "}')
 
#if not, try this...
i=0
for val in $(tail -200 /home/log.txt | grep -A6 'Header' | grep -v 'Header' | awk '{printf $(NF-1)" "}')
do
  arr[$i]=$val
  ((i=i+1))
done

Code:
echo ${arr[0]}
echo ${arr[1]}
...

If you can provide the exact data, may be we can simplify the grep commands for you...

--ahamed
# 3  
Old 12-15-2011
Thanks Ahamed101 it worked great. I could not use the set but I was able to use your second suggestion.
I think the greping is okay since it works, but thanks for offering to help with that.

only thing was that I had to change arr[$i]=$val to arr[i]=$val.

Could you tell me what awk '{printf $(NF-1)" "}' does?

Last edited by Filter500; 12-15-2011 at 09:05 AM.. Reason: Mistake
# 4  
Old 12-15-2011
Quote:
Originally Posted by Filter500
Could you tell me what awk '{printf $(NF-1)" "}' does?
It prints the last-but-one field in a line, followed by a space.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

2. Shell Programming and Scripting

Rearrange or replace only the second line after pattern match or pattern match

Im using the command below , but thats not the output that i want. it only prints the odd and even numbers. awk '{if(NR%2){print $0 > "1"}else{print $0 > "2"}}' Im hoping for something like this file1: Text hi this is just a test text1 text2 text3 text4 text5 text6 Text hi... (2 Replies)
Discussion started by: invinzin21
2 Replies

3. UNIX for Dummies Questions & Answers

Match Pattern after certain pattern and Print words next to Pattern

Hi experts , im new to Unix,AWK ,and im just not able to get this right. I need to match for some patterns if it matches I need to print the next few words to it.. I have only three such conditions to match… But I need to print only those words that comes after satisfying the first condition..... (2 Replies)
Discussion started by: 100bees
2 Replies

4. Shell Programming and Scripting

Store value in array with awk

Hi everybody I wanna store some values that r in a .txt file in some arrays for example I have: 32782 28 32783 02 32784 01 32785 29 32786 25 32787 25 32788 00 32789 25 32790 02 32791 29 32792 23 32793 01 32794 28 and I need to save the first... (4 Replies)
Discussion started by: Behrouzx77
4 Replies

5. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

6. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

7. Shell Programming and Scripting

Store all the passed arguments in an array and display the array

Hi I want to write a script which store all the parameters passed to the script into an array. Once it is stored I want scan through the array and and delete those files for last month present inside the directory. The files in directory is appneded with YYYY_MM_DD. I want to know how can I... (3 Replies)
Discussion started by: dgmm
3 Replies

8. Shell Programming and Scripting

Match first pattern first then extract second pattern match

My input file: <accession>Q91G55</accession> <name>043L_IIV6</name> <protein> <recommendedName> <location> <position position="294"/> </location> <fullName>Uncharacterized protein 043L</fullName> <accession>P18556</accession> <name>1106L_ASFB7</name> <protein> <recommendedName>... (5 Replies)
Discussion started by: patrick87
5 Replies

9. Shell Programming and Scripting

Perl Array / pattern match large CPU usage

Hi, I have one file in this format 20 value1 33 value2 56 value3 I have another file in this format: 34,30-SEP-09,57,100237775,33614510126,2,34 34,30-SEP-09,57,100237775,33620766654,2,34 34,30-SEP-09,108,100237775,33628458122,2,34 34,30-SEP-09,130,100237775,33635266741,2,254... (6 Replies)
Discussion started by: Donkey25
6 Replies

10. UNIX for Dummies Questions & Answers

trying to store variables in an array

im looping through an array setting three variables each time (one of the variables gives me the position in the array and is incremented each loop) im trying to then set the variables to that position in the array without much luck. any ideas? anArray=`$VAR1+$VAR2+"("$pos")"` (1 Reply)
Discussion started by: magnia
1 Replies
Login or Register to Ask a Question