Search for Pattern as output between the matched String


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for Pattern as output between the matched String
# 1  
Old 02-26-2016
Search for Pattern as output between the matched String

Hello,

I have a file which has the below contents :

Code:
VG_name           LV_name             LV_size in MB    LV_option           LV_mountpoint                owner   group y
testdg                     rahul2lv        10                  "-A y -L"                  /home/abc2                         oracle    dba

I want to print only the pattern between double quotes. In the above case output should be following : -A y -L

Please help me to achieve this.

Thanks
Rahul
# 2  
Old 02-26-2016
Hello rahul2662,

Following may help you in same, also you haven't mentioned if there are 1 or more occurrences of "" apart from shown in your sample post in a single line, so assuming you have only 1 occurrence of "" in a single line.
Code:
awk '{match($0,/".*"/);A=substr($0,RSTART+2,RLENGTH-3);if(A){print A}}'  Input_file

Output will be as follows.
Code:
A y -L

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 02-26-2016
Hello Ravinder ,

Yes there is only one occurence of "".

Also is there any simpler sed command to achieve this ?

Could you please explain following (RSTART and RLENGTH part) : A=substr($0,RSTART+2,RLENGTH-3)

Also thanks a lot for your help and this worked.

Thanks
Rahul
# 4  
Old 02-26-2016
Hello rahul2662,

Could you please try following sed solution and let me know if this helps you.
Code:
sed -n 's/\(.*"\)\([^"].*\)\(".*\)/\2/p'  Input_file

Output will be as follows.
Code:
-A y -L

EDIT: Sorry was busy in a deployment so couldn't give explanation for code posted in POST#2. Following is the explanation for code posted in POST#2 as follows.
Code:
awk '{match($0,/".*"/);          ###### using match keyword in-built awk functionality for matching the regex into Input_file. Here patteren is match(LINE,REGEX).
A=substr($0,RSTART+2,RLENGTH-3); ###### creating a variable A whose value is equal to substring of line's starting point which is RSTART+2 and line's end point which is RLENGTH-3. NOTE: variables RSTART and RLENGTH are again awk's inbuilkt variables which will get values once a regex match is TRUE while using match keyword.
if(A){print A}}                  ###### Making sure here value of A is present then printing the value of variable A then.
' Input_file                     ###### mentioning the Input_file here for same.

Thanks,
R. Singh

Last edited by RavinderSingh13; 02-26-2016 at 07:25 AM.. Reason: Added explanation for code posted by me in POST#2 as per user's request.
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 02-26-2016
Try also
Code:
awk -F\" '{for (i=2; i<=NF; i+=2) print $i}' file
-A y -L

This User Gave Thanks to RudiC For This Post:
# 6  
Old 02-26-2016
If there is never more than one quoted string on a line, the awk can be simplified to:
Code:
awk -F\" 'NF>2{print $2}' file

and the sed could be simplified to:
Code:
sed -n 's/.*"\(.*\)".*/\1/p'  file

This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 02-27-2016
Code:
awk '/\"/{for(i=2;i<=NF;i+=2){print $i}}' FS=\"  file

This User Gave Thanks to danmero For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. UNIX for Dummies Questions & Answers

Search String, Out matched text and input text for no match.

I need to search a string for some specific text which is no big deal using grep. My problem is when the search fails to find the text. I need to add text like "na" when my search does not match. I have tried this command but it does not work when I put the command in a loop in a bash script: ... (12 Replies)
Discussion started by: jojojmac5
12 Replies

3. Shell Programming and Scripting

Use search pattern to reformat the output

I have below file listing ] ls -1 *.txt MISTradesReport_141105_d130240_VOLCKER_EMEA_LOANIQ_FEED_2013-12-24.txt MISTradesReport_141106_d130240_VOLCKER_NA_LOANIQ_FEED_2013-12-24.txt MISTradesReport_141107_d130240_VOLCKER_EMEA_CDS_CRDI_FEED_2013-12-24.txt... (4 Replies)
Discussion started by: krg.sati
4 Replies

4. Shell Programming and Scripting

How to use sed to search a particular pattern in a file backward after a pattern is matched.?

Hi, I have two files file1.txt and file2.txt. Please see the attachments. In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to... (9 Replies)
Discussion started by: saurabh kumar
9 Replies

5. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

6. Shell Programming and Scripting

Help required on joining one line above & below to the pattern matched string line.

Hi Experts, Help needed on joining one line above & below to the pattern matched string line. The input file, required output is mentioned below Input file ABCD DEFG5 42.0.1-63.38.31 KKKK iokl IP Connection Available ABCD DEFG5 42.0.1-63.38.31 ... (7 Replies)
Discussion started by: krao
7 Replies

7. Shell Programming and Scripting

Extracting particular string in a file and storing matched string in output file

Hi , I have input file and i want to extract below strings <msisdn xmlns="">0492001956</ msisdn> => numaber inside brackets <resCode>3000</resCode> => 3000 needs to be extracted <resMessage>Request time getBalances_PSM.c(37): d out</resMessage></ns2:getBalancesResponse> => the word... (14 Replies)
Discussion started by: sushmab82
14 Replies

8. UNIX for Dummies Questions & Answers

How to search for a pattern a string?

Hi all, I'm new in UNIX , so how to check if stringA is present within stringB ? Something similar to INSTR function in pl sql... Thanks a lot. (12 Replies)
Discussion started by: Leo_NN
12 Replies

9. 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

10. Shell Programming and Scripting

Advance string pattern search Please

Here is my problem.. 1. I want to search all those files with file name starting AJ128**** (in all the sub directories also) 2. I want to search for the follwoing type of string line beging with string - 'AK*any_1_char*any_2_char*510' 3. I need to display list of file names... (2 Replies)
Discussion started by: sainj
2 Replies
Login or Register to Ask a Question

Featured Tech Videos