Search and retreive after matched words


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search and retreive after matched words
# 1  
Old 08-27-2015
Search and retreive after matched words

I have following source file format

Code:
{"driver":{"first_name":"xxxx","last_name":"yyyy"},"confirmation_id":"US285204420","vendor":{"id":"1234","name":"BUDGET"}}

I need to extract the data from above and provide in below desired output is

Code:
xxxx,yyyy,US285204420,1234,BUDGET

Can you please advise ?

Last edited by Scrutinizer; 08-27-2015 at 11:33 AM.. Reason: code tags
# 2  
Old 08-27-2015
Code:
perl -ne 'print "$1,$2,$3,$4,$5\n" if /{"driver":{"first_name":"([^"]+)","last_name":"([^"]+)"},"confirmation_id":"([^"]+)","vendor":{"id":"([^"]+)","name":"([^"]+)"}}/' filename

ASSUMPTIONS, the format is one record per line and 1 2345 should have been 12345 in the source data
This User Gave Thanks to Skrynesaver For This Post:
# 3  
Old 08-27-2015
Thank you.

Your assumptions are correct, it is one record per line and its 12345.
Is there a way to route this to a file?

Also There is a date field in another text file.I need to append the date field to this output at the end.


b)Also is there a Unix code equivalent as I am not too familiar with Perl.Seems Perl implementation is very easy.
# 4  
Old 08-27-2015
Try also
Code:
awk -F\" '/^{"driver.*first_name.*last_name.*}}$/ {print $6, $10, $14, $20, $24}' OFS=, file
xxxx,yyyy,US285204420,1 234,BUDGET

This User Gave Thanks to RudiC For This Post:
# 5  
Old 08-27-2015
Could you please dissect the code to what this does?
# 6  
Old 08-27-2015
Quote:
Originally Posted by nqp200
Could you please dissect the code to what this does?
Hello nqp200,
Code:
 awk -F\" '/^{"driver.*first_name.*last_name.*}}$/ {print $6, $10, $14, $20, $24}' OFS=, file

Here -Fmeans it is the field separator which is character " but we used \"to escape it as many characters have their special meaning so we are escaping it. Then Regex /^{"driver.*first_name.*last_name.*}}$/means a line which starts from word driver and then have words first_name and last_name in them and ends with character }}, if this happens then perform action print with fields 6th, 10th, 14th, 20th and 24th field. OFSmeans Output field separator which is ,.

Hope this helps.

Thanks,
R. Singh
# 7  
Old 08-27-2015
how is the 6th position determined likewise 10,14,24
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Count exact matched words

hi , i have a file test.dat which contains following data. test.dat XY|abc@xyz.com XY|abc@xyz.com ST|abc@xyz.com ST|abc@xyz.com ST|XYZ@abc.com FK|abc@xyz.com FK|STG@xyz.com FK|abc@xyz.com FK|FKG@xyz.com i want to know the count of XY,ST,FK. i.e XY = 2 , ST = 3 , FK = 4 I am... (4 Replies)
Discussion started by: itzkashi
4 Replies

2. UNIX for Beginners Questions & Answers

Search a string inside a pattern matched block of a file

How to grep for searching a string within a begin and end pattern of a file. Sent from my Redmi 3S using Tapatalk (8 Replies)
Discussion started by: Baishali
8 Replies

3. Shell Programming and Scripting

Search words in any quote position and then change the words

hi, i need to replace all words in any quote position and then need to change the words inside the file thousand of raw. textfile data : "Ninguno","Confirma","JuicioABC" "JuicioCOMP","Recurso","JuicioABC" "JuicioDELL","Nulidad","Nosino" "Solidade","JuicioEUR","Segundo" need... (1 Reply)
Discussion started by: benjietambling
1 Replies

4. Shell Programming and Scripting

Search for Pattern as output between the matched String

Hello, I have a file which has the below contents : VG_name LV_name LV_size in MB LV_option LV_mountpoint owner group y testdg rahul2lv 10 "-A y -L" /home/abc2 ... (6 Replies)
Discussion started by: rahul2662
6 Replies

5. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

6. Shell Programming and Scripting

Perl - use search keywords from array and search a file and print 3rd field when matched

Hi , I have been trying to write a perl script to do this job. But i am not able to achieve the desired result. Below is my code. my $current_value=12345; my @users=("bob","ben","tom","harry"); open DBLIST,"<","/var/tmp/DBinfo"; my @input = <DBLIST>; foreach (@users) { my... (11 Replies)
Discussion started by: chidori
11 Replies

7. Shell Programming and Scripting

Search between two words

Hello, I try to print out with sed or awk the 21.18 between "S3 Temperature" and "GrdC" in a text file. The blanks are all real blanks no tabs. Only the two first chars from temperture are required. So the "21" i need as output. S3 Temperatur 21.18 GrdC No Alarm ... (3 Replies)
Discussion started by: felix123
3 Replies

8. Shell Programming and Scripting

Better and efficient way to reverse search a file for first matched line number.

How to reverse search for a matched string in a file. Get line# of the first matched line. I am getting '2' into 'lineNum' variable. But it feels like I am using too many commands. Is there a better more efficiant way to do this on Unix? abc.log aaaaaaaaaaaaa bbbbbbbbbbbbb... (11 Replies)
Discussion started by: kchinnam
11 Replies

9. Shell Programming and Scripting

Print two matched words from the same line

Hi experts I need to pick 2 matched words from the same line..... I have given below an example file eg: O14757 hsa04110 hsa04115 2 P38398 hsa04120 1 O15111 hsa04010 hsa04210 hsa04920 hsa04620 hsa04660 hsa04662 hsa05200 hsa05212 hsa05221 hsa05220 hsa05215 hsa05222 hsa05120 13 O14920... (4 Replies)
Discussion started by: binnybio
4 Replies

10. Shell Programming and Scripting

search for the matched pattern by tracing back from the line

Hi, I want to grep the line which has 'data11'.then from that line, i need to trace back and find out the immediate line which has the same timestamp of that grepped line. for eg: log file: ----------- Process - data Process - datavalue - 2345 Process - data Process - data Process... (9 Replies)
Discussion started by: Sharmila_P
9 Replies
Login or Register to Ask a Question