Search and retreive after matched words


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search and retreive after matched words
# 8  
Old 08-27-2015
Quote:
Originally Posted by nqp200
how is the 6th position determined likewise 10,14,24
With " as the field separator, your sample input is shown on the first line below and the field numbers are shown on the next line (with the leading digit dropped in field numbers 13, 15, 19, 21, and 23 since I couldn't fit two digits under those one character fields):
Code:
{"driver":{"first_name":"xxxx","last_name":"yyyy"},"confirmation_id":"US285204420","vendor":{"id":"1234","name":"BUDGET"}}
1"     2" 3"         4"5"   6"7"        8"9"  10"11"             12"3"         14"5"    16"17"18"9"  20"1"  22"3"    24"25

Does this help?
This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 08-27-2015
thanks Don!
# 10  
Old 08-27-2015
Quote:
Originally Posted by nqp200
how is the 6th position determined likewise 10,14,24
At times, it is best to let the application tell you. With awk you can iterate over the record.

Code:
$echo '{"driver":{"first_name":"xxxx","last_name":"yyyy"},"confirmation_id":"US285204420","vendor":{"id":"1234","name":"BUDGET"}}' | 
awk -F\" '{for (i=1; i<=NF; i++){print "$" i " = " $i}}'
$1 = {
$2 = driver
$3 = :{
$4 = first_name
$5 = :
$6 = xxxx
$7 = ,
$8 = last_name
$9 = :
$10 = yyyy
$11 = },
$12 = confirmation_id
$13 = :
$14 = US285204420
$15 = ,
$16 = vendor
$17 = :{
$18 = id
$19 = :
$20 = 1234
$21 = ,
$22 = name
$23 = :
$24 = BUDGET
$25 = }}

# 11  
Old 08-30-2015
Before closing this thread I have one more question I need to create separate types of files for databeing retrieved from this dataset.
Input excel file has
3 unique ids each in a cell and the above dataset in one cell..with above code I am able to get the data extract.However my input filehas
id, memberid,ititnid,unparsingdataset

Now if I need to split this xlsx file to retrieve only unparsed dataset and add one unique id to this dataset ,what should be possible logic.
1.I am using this

restructured the layout using "
Code:
awk -v val=$num -F'"' '{print $4 "|" $1 "|" $2 "|" $3  }' Car.xlsx>Car2new.xlsx>Car.xlsx

2.enerate on file for one type of dataset; will this work..adding unique id to each file so that i can join this after adding to database table


Code:
awk -F\" '/^{"driver.*first_name.*last_name.*}}$/ {print $6, $10, $14, $260}’ OFS=, CarReservationsNov2.xlsx > Customer.xlsx*

awk -F\" '/^{"driver.*first_name.*last_name.*}}$/ {print $20, $24, $34,$14,$260}’ OFS=, CarReservationsNov2.xlsx > vendor.xlsx*

where


Code:
$20 = 1363
$21 = ,
$22 = name
$23 = :
$24 = BUDGET
$25 = ,
$26 = image
$27 = :
$28 = https
$29 = },
$30 = pick_up
$31 = :{
$32 = date_time
$33 = :
$34 = 2014-11-01T12:00:00+00:00
$260=5451  id 
$261=c870efa6-6184-11e4-8df3-121e6b429d9a memberid
$262=39556itiid


Last edited by Don Cragun; 08-30-2015 at 05:50 PM.. Reason: Add CODE tags.
# 12  
Old 08-30-2015
Quote:
Originally Posted by nqp200
Before closing this thread I have one more question I need to create separate types of files for databeing retrieved from this dataset.
Input excel file has
3 unique ids each in a cell and the above dataset in one cell..with above code I am able to get the data extract.However my input filehas
id, memberid,ititnid,unparsingdataset
First: awk processes text files; not MicroSoft Excel files. I, therefore, assume that you have used Excel to extract the data from that spreadsheet file into a CSV formatted text file and named that file with a .xlsx file extension instead of a more appropriate .txt file extension. Is my assumption correct?

Note also that awk does not produce Excel spreadsheets as output files. Although UNIX and Linux systems don't determine file types by pathname extensions, naming a file with an extension that does not match the format associated with that filename extension will confuse anyone familiar with the traditional contents of a MicroSoft excel spreadsheet file with the .xlsx filename extension.

Second: What is the format of the "3 unique ids each in a cell" and the format of "the above dataset in one cell"?

Quote:
Now if I need to split this xlsx file to retrieve only unparsed dataset and add one unique id to this dataset ,what should be possible logic.
1.I am using this

restructured the layout using "
Code:
awk -v val=$num -F'"' '{print $4 "|" $1 "|" $2 "|" $3  }' Car.xlsx>Car2new.xlsx>Car.xlsx

The above code destroys any data that may have existed in the file Car.xlsx and creates two empty files Car.xlsx and Car2new.xlsx. Even if you didn't redirect output to destroy your input file, this awk script does not create any unique IDs; it just moves the 4th input field to be the 1st output field, and changes field separators from the double-quote character to the vertical-bar character. You pass a number to it as a parameter, but val is never used in your script. Please explain, in English, what you are trying to do and show us sample input and the desired output corresponding to that input (both in CODE tags).
Quote:
2.enerate on file for one type of dataset; will this work..adding unique id to each file so that i can join this after adding to database table


Code:
awk -F\" '/^{"driver.*first_name.*last_name.*}}$/ {print $6, $10, $14, $260}' OFS=, CarReservationsNov2.xlsx > Customer.xlsx*

awk -F\" '/^{"driver.*first_name.*last_name.*}}$/ {print $20, $24, $34,$14,$260}' OFS=, CarReservationsNov2.xlsx > vendor.xlsx*

where


Code:
$20 = 1363
$21 = ,
$22 = name
$23 = :
$24 = BUDGET
$25 = ,
$26 = image
$27 = :
$28 = https
$29 = },
$30 = pick_up
$31 = :{
$32 = date_time
$33 = :
$34 = 2014-11-01T12:00:00+00:00
$260=5451  id 
$261=c870efa6-6184-11e4-8df3-121e6b429d9a memberid
$262=39556itiid

What you are trying to do with > vendor.xlsx* is not clear. Using a character in an output filename that is a special character in shell pathname expansion is likely to cause you lots of problems later. If you are attempting to cause the awk script to produce multiple output files, that is not the way to do it. If there are other existing file with names starting with the string vendor.xlsx (or Customer.xlsx) you will be directing the output to the 1st match file and using the other matched files as additional input file to your script. Please clearly describe what files you are trying to produce and show us the content that you want to appear in each of those files (in CODE tags).
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