extract key values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract key values
# 1  
Old 08-06-2010
extract key values

I am parsing a log with key values spread all over in the following fashion:

Code:
TEST 1 SCHEME 12 SET EMPTY
VARLEN SET TEST 1201 PARAM1 EMTY PARAM2 SET
SCHEME 12 REFRESH TEST 8

I need to extract test number, my result should be
Code:
1
1201
8

I use awk for processing this log and use split to search for the values:
Code:
n = split($0, a);
for(i = 1; i <= n; i++) {
        if(a[i] == "TEST") {
                value = a[i+1]; 
        }       
}

I suspect my code is not efficient, I thought of using index($0, "TEST") within substr etc.., but my code ended up using two index() calls and this is not the best way either.

Any ideas how to improve the code would be appreciated.
# 2  
Old 08-06-2010
grep & regex
Code:
grep -o 'TEST [0-9]*' file | cut -d' ' -f2

# 3  
Old 08-06-2010
If your grep doesn't support -o option:

Code:
sed -n '/TEST [0-9][0-9]*/ s/.*TEST \([0-9][0-9]*\).*/\1/p' file

# 4  
Old 08-06-2010
Anchal

The command is good .
can u explain the command?
# 5  
Old 08-06-2010
Code:
'/TEST [0-9][0-9]*/

searching for the pattern. TEST<space><one or more digits>

Code:
s/.*TEST \([0-9][0-9]*\).*/\1/p'

removing everything but the required numbers from the above matched lines.

\(..\) are used to save the regex/pattern found.
\1 are used to recall the above regex/pattern.

you can have max 9 such recalls.
This User Gave Thanks to clx For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Taking key values from one file and extracting values from another file

Hi, I have two files with values in both. File1: cat 2 3 dog 4 5 elephant 6 7 camel 2 3 File2: ----+--gkf;ajf= ---+---- +----- cat -------=----+ 3 | 4 ----- dog ------++-- 5 | 9 ----++-- elephant | 5 | 7 ---++ camel ------ ++++_---- || 8 | 9 I want the final file as: cat 4... (1 Reply)
Discussion started by: npatwardhan
1 Replies

2. Shell Programming and Scripting

Fetch the values based on a Key using awk from single file

Hi, Please help to fetch the values for a key from below data format in linux. Sample Input Data Format 11055005|PurchaseCondition|GiftQuantity|1 11055005|PurchaseCondition|MinimumPurchase|400 11055005|GiftCatalogEntryIdentifier|Id|207328014 11429510|PurchaseCondition|GiftQuantity|1... (2 Replies)
Discussion started by: mohanalakshmi
2 Replies

3. Shell Programming and Scripting

Show distinct values of a key from a single line

Hi All, I am newbie to linux. Can somebody help me with following requirement. I have one huge line. I have to find out particular key/value pair to see the distinct value of that key. Portion of the String:- <?xml version="1.1" encoding="UTF-8"?> <Data><Val Ti="1342750845538" Du="0"... (5 Replies)
Discussion started by: kmajumder
5 Replies

4. Shell Programming and Scripting

Extract key words and print their values

Input file (HTTP request log file): GET... (2 Replies)
Discussion started by: buptwy
2 Replies

5. Shell Programming and Scripting

remove 1st and last last values of a key

For every specific keys i.e. 1st and 4th columns (a1 and ABC_001144992) remove 1st and last value (bold ones - 87942437 and 87952030 ) and print remaining input a1 87942437 87943147 1E ABC_001144992 a1 87945162 87945276 2E ABC_001144992 a1 87949524 87952030 3E ABC_001144992 a1... (3 Replies)
Discussion started by: repinementer
3 Replies

6. Shell Programming and Scripting

Print a key with its all values using awk/others

input COL1 a1 b1 c1 d1 e1 f1 C1 10 10 10 100 100 1000 C2 20 20 200 200 200 2000 output C1 a1 10 1 C1 b1 10 1 C1 c1 10 1 C1 d1 100 2 C1 e1 100 2 C1 f1 1000 3 C2 ... (12 Replies)
Discussion started by: ruby_sgp
12 Replies

7. Shell Programming and Scripting

How to pick values from column based on key values by usin AWK

Dear Guyz:) I have 2 different input files like this. I would like to pick the values or letters from the inputfile2 based on inputfile1 keys (A,F,N,X,Z). I have done similar task by using awk but in that case the inputfiles are similar like in inputfile2 (all keys in 1st column and values in... (16 Replies)
Discussion started by: repinementer
16 Replies

8. Shell Programming and Scripting

ksh scripting: Extract 1 most recent record for unique key

I'm loading multiple delimited files into an Oracle DB using sqlldr on Unix. I would like to get only the most recent record per each unique key. There may be multiple updates for each key, but I only want the most recent one. There is a date column in my delimited files, so I'm using cat to... (2 Replies)
Discussion started by: OPTIMUS_prime
2 Replies

9. Shell Programming and Scripting

How to delete ctrl key values in a given string?

Hi all, My query is... in the runtime, you are getting any input string. Unfortunately, you have pressed some ctrl keys or esc keys or arrow keys while typing input string. You can get the input value like that... input string as welcome^ So ,I want to remove those unwanted keys... (4 Replies)
Discussion started by: balan_mca
4 Replies

10. UNIX for Dummies Questions & Answers

Generating key values for leader records

All, I have a file with text as shown below. I want the o/p file with generated values in the first column as shown in the o/p file. Pls note that the size of my file is 6 GB. How do i do this ? Input file 999999abcdef 999999ghijkl 999999mnopq 777777rosesarered 777777skyisblue Output... (1 Reply)
Discussion started by: ajfaq
1 Replies
Login or Register to Ask a Question