Extract values in a line using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract values in a line using awk
# 1  
Old 06-07-2016
Extract values in a line using awk

hello all,

I need your help in extracting values of some parameter within a line using awk.

for example:

i have the below line available in a file and i want to extract the values of only CustomerId, s_PackageId and s_HZINumbers in order the result to be as

Code:
967666666666|"HomeZone"|"17103","10803","100000","200000","300000"

the file called test.file which has the contents as below:

Code:
Read:RPP(CustomerId="967666666666",OfferProfileKey=1,Key=7,category=ONLINE,prefetchFilter=-1,s_ActivationEndTime=32532613199999,s_ActivationStartTime=1459112400000,s_CRMTitle="-",s_CanBeSharedByMultipleRops=FALSE,s_InsertedViaBatch=TRUE,s_PackageId="HomeZone",s_PeriodStartPoint=0,vValidFrom=2016-03-28 00:00:00,vInvalidFrom=4000-01-01 00:00:00,s_FreeOfChargeModifyTasks=0,s_HZINumbers=["17103","10803","100000","200000","300000"],s_PeriodicBonus={CreditLimit=0,CreditMax=1000000},s_Touched=TRUE,bCategory=ONLINE,bSeriesId=0,bValidFrom=2009-01-01 00:00:00,bInvalidFrom=MAX_DATEANDTIME,s_Active=TRUE,s_ExpireDate=3000-01-01,s_FreeOfChargePeriods=0,s_NextPeriodAct=32532613199999,s_OnTouchDate=32532613199999,s_StartDate=1970-01-01,s_Valid=TRUE);

i used the below command

Code:
awk '{if (!match($0, /CustomerId=([-0-9a-zA-Z]*)/)) next; cust = (substr($0, RSTART + 12, RLENGTH + 1));if (!match($0, /s_PackageId="HomeZone"/)) next; pkg = (substr($0, RSTART + 12, RLENGTH -12));if (!match($0, /s_HZINumbers=([-0-9a-zA-Z,]*)/)) next; hzin = (substr($0, RSTART + 14, RLENGTH - 14));printf("%s|%s|%s\n", cust,pkg,hzin); }' test.file

i get the result for customerid, and package only. the result for s_HZINumbers is not appearing.

Code:
967666666666|"HomeZone"|

i would appreciate it if you get it solved for me,

Thank you

Moderator's Comments:
Mod Comment Use code tags, thanks.

Last edited by zaxxon; 06-07-2016 at 10:30 AM..
# 2  
Old 06-07-2016
You can try this with sed:
Code:
$ sed 's/.*CustomerId="\([^"]\+\)".*s_PackageId=\([^,]\+\).*s_HZINumbers=\[\([^]]\+\)\].*/\1|\2|\3/g' infile
967666666666|"HomeZone"|"17103","10803","100000","200000","300000"

This User Gave Thanks to zaxxon For This Post:
# 3  
Old 06-07-2016
i have so many lines inside the file with different customerid and packageid values. i want to specify that if the packageid="HomeZone" then i need s_HZINumbers values to be listed along with the customerid, packageid and s_HZINumbers.
# 4  
Old 06-07-2016
Hello nael_najib,

Could you please try following and let me know if this helps you.
Code:
awk '{match($0,/CustomerId=/);VAL=substr($0,RSTART+12,RLENGTH);match($0,/s_PackageId=/);VAL=VAL "|" substr($0,RSTART+12,RLENGTH-2);match($0,/HZINumbers=\[[^\]]*/);print VAL "|" substr($0,RSTART+12,RLENGTH-12)}'   Input_file

Output will be as follows.
Code:
96766666666|"HomeZone"|"17103","10803","100000","200000","300000"

EDIT: Adding a non-one liner form of solution.
Code:
awk '{
        match($0,/CustomerId=/);
        VAL=substr($0,RSTART+12,RLENGTH);
        match($0,/s_PackageId=/);
        VAL=VAL "|" substr($0,RSTART+12,RLENGTH-2);
        match($0,/HZINumbers=\[[^\]]*/);
        print VAL "|" substr($0,RSTART+12,RLENGTH-12)
     }
    '   Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 06-07-2016 at 11:20 AM.. Reason: Added a non-one liner form of solution now.
# 5  
Old 06-07-2016
Thank you RavinderSingh13. i actually need to make (if) condition when specifying the ( packageid="HomeZone" ) or otherwise it will list lots of entries.
# 6  
Old 06-07-2016
Hello nael_najib,

Your welcome, could you please try following and let me know how it goes then.
Code:
awk '{match($0,/CustomerId=/);VAL=substr($0,RSTART+12,RLENGTH);match($0,/s_PackageId=/);Q=substr($0,RSTART+12,RLENGTH-2);VAL=VAL "|" Q;match($0,/HZINumbers=\[[^\]]*/);if(Q == "\"HomeZone\""){print VAL "|" substr($0,RSTART+12,RLENGTH-12)}}'   Input_file

EDIT: Adding a non-one liner form of above solution too now.
Code:
awk '{
        match($0,/CustomerId=/);
        VAL=substr($0,RSTART+12,RLENGTH);
        match($0,/s_PackageId=/);
        Q=substr($0,RSTART+12,RLENGTH-2);
        VAL=VAL "|" Q;
        match($0,/HZINumbers=\[[^\]]*/);
                if(Q == "\"HomeZone\""){
                                        print VAL "|" substr($0,RSTART+12,RLENGTH-12)
                                       }
     }
    '     Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 06-07-2016 at 11:26 AM.. Reason: Adding a non-one liner form of solution now too.
This User Gave Thanks to RavinderSingh13 For This Post:
# 7  
Old 06-07-2016
It worked now. I really appreciate the good and fast response RavinderSingh13. you are really great
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl to extract values and print at end of each line

In the below perl I am trying to extract and print the values AF1=, the GT value, and F or QUAL diveded by 33 (rounded to the nearest whole #). The GT value is at the end after the GT:PL so all the possibilities are read into a hash h, then depending on the value that is in the line the... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. Shell Programming and Scripting

awk to match file1 and extract specific tag values

File2 is tab-delimeted and I am trying to use $2 in file1 (space delimeted) as a search term in file2. If it is found then the AF= in and the FDP= values from file2 are extracted and printed next to the file1 line. I commented the awk before I added the lines in bold the current output resulted. I... (7 Replies)
Discussion started by: cmccabe
7 Replies

3. Shell Programming and Scripting

awk to extract multiple values from file and add two additional fields

In the attached file I am trying to use awk to extract multiple values and create the tab-delimited desired output. In the output R_Index is a the sequential # and Pre_Enrichment is defaulted to .. I can extract from the values to the side of the keywords, but most are above and I can not... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

How to extract xml attribute values using awk inline.?

I am trying to extract specific XML attribute values for search pattern <factories.*baseQueueName' from resources.xml. my scripts works ok,, but to extract 3 values this code does echo $line three times, it could be 'n' times. How can I use awk to extract matching pattern values in-line or... (11 Replies)
Discussion started by: kchinnam
11 Replies

5. Shell Programming and Scripting

Extract values froma line in file

p.txt T|DCNT=100|RECCHK=22222.2|PERCHK=32323|# I want to extract the value of 100 22222.2 and 32323 and assign it to variable x1,y1,z1 x=`cut -f2 -d "=" p.txt` x1=`echo $x | cut -f1 -d "|" ` y=`cut -f3 -d "=" p.txt` y1=`echo $y | cut -f1 -d "|" ` z=`cut -f4 -d "=" p.txt` z1=`echo $z... (3 Replies)
Discussion started by: w020637
3 Replies

6. Shell Programming and Scripting

How to extract part of xml line via awk?

Hi, I like to set a variable "name" automatically by reading an xml file. The name should be set to the date, which is a part of the following line of the xml file: <sceneID>C82_N32_A_SM_strip_008_R_2009-11-24T04:22:12.790028Z</sceneID> How can I separate this line, that the name will... (6 Replies)
Discussion started by: friend
6 Replies

7. Shell Programming and Scripting

how to extract part of xml line via awk?

Hi, I like to set a variable "name" automatically by reading an xml file. My code looks like this: set name = `awk '/<generationTime>/,/<\/generationTime>/ p' $xml_name` the "name" is thus set to <generationTime>2004-12-01T08:23:50.000000</generationTime> How can I separate this line,... (3 Replies)
Discussion started by: friend
3 Replies

8. Shell Programming and Scripting

Awk extract a range of values

Hi Input 10 131 11 179 11 170 20 142 20 131 20 144 21 178 22 155 22 196 23 144 23 184 24 194 24 191 24 218 25 167 25 131 26 189 (6 Replies)
Discussion started by: genehunter
6 Replies

9. UNIX for Dummies Questions & Answers

how extract certain value within a line using awk

hi if would like to get the phone number as an output, can you guide me here please <A>213444555</A><B><B>ABCDEFG</B> I just want to get the phone number from the file in between <A> and </A> Thanks (9 Replies)
Discussion started by: imran721
9 Replies

10. Shell Programming and Scripting

awk: need to extract a line before a pattern

Hello , I need your help to extract a line in a big file , and this line is always 11 lines before a specific pattern . Do you know a way via Awk ? Thanks in advance npn35 (17 Replies)
Discussion started by: npn35
17 Replies
Login or Register to Ask a Question