Unix/Linux Go Back    


UNIX for Beginners Questions & Answers If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

Regex to identify pattern

UNIX for Beginners Questions & Answers


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-10-2017   -   Original Discussion by dashing201
dashing201 dashing201 is offline
Registered User
 
Join Date: Nov 2009
Last Activity: 10 October 2017, 8:13 AM EDT
Posts: 81
Thanks: 13
Thanked 0 Times in 0 Posts
Regex to identify pattern

Hi
In a file I have string in multiple lines. Like below:

Code:
<?=test.getObjectName("L", "testTBL","D") ?> 
<?=test.getObjectName("L", "testTBL","testDB", "D") ?>

I want to use regex to search for the pattern "<?=test.getObjectName...?>"
If the parenthesis has 3 parameters then return 2nd one.
If the parenthesis has 4 parameters then retrun 3rd&2nd.
So expected output:

Code:
testTBL
testDB.testTBL

Thanks in advance
Nitin
Sponsored Links
    #2  
Old Unix and Linux 10-10-2017   -   Original Discussion by dashing201
Scott's Unix or Linux Image
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 23 November 2017, 2:35 AM EST
Location: Zürich
Posts: 8,839
Thanks: 368
Thanked 1,204 Times in 1,028 Posts
Assuming the input is fixed, as you described

Code:
awk -F"[, ]" '
 { O=(NF>6)?$4"."$3:$3; gsub("\"", "", O); print O }
' test.txt

testTBL
testDB.testTBL

Sponsored Links
    #3  
Old Unix and Linux 10-10-2017   -   Original Discussion by dashing201
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 November 2017, 1:45 PM EST
Location: Aachen, Germany
Posts: 11,644
Thanks: 321
Thanked 3,619 Times in 3,324 Posts
Try also

Code:
sed 's/<?=test.getObjectName("[^"]*",//; s/, *"D") *?>//; s/[" ]//g; s/\([^,]*\),\([^,]*\)/\2.\1/ ' file
testTBL
testDB.testTBL

The Following User Says Thank You to RudiC For This Useful Post:
dashing201 (10-10-2017)
    #4  
Old Unix and Linux 10-10-2017   -   Original Discussion by dashing201
dashing201 dashing201 is offline
Registered User
 
Join Date: Nov 2009
Last Activity: 10 October 2017, 8:13 AM EDT
Posts: 81
Thanks: 13
Thanked 0 Times in 0 Posts
Hi Scott
thanks for prompt reply.
However I am specifically looking for regex as the same code may be executed in unix shell or other languages.

Thanks
Nitin
Sponsored Links
    #5  
Old Unix and Linux 10-10-2017   -   Original Discussion by dashing201
Scott's Unix or Linux Image
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 23 November 2017, 2:35 AM EST
Location: Zürich
Posts: 8,839
Thanks: 368
Thanked 1,204 Times in 1,028 Posts
Good then then RudiC just gave you one Linux It's not pretty, but they often aren't!
Sponsored Links
    #6  
Old Unix and Linux 10-10-2017   -   Original Discussion by dashing201
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 November 2017, 1:45 PM EST
Location: Aachen, Germany
Posts: 11,644
Thanks: 321
Thanked 3,619 Times in 3,324 Posts
Please be aware that a regex does not "return" anything but only matches something. Those matches may be deleted, substituted, shoved around, whatsoever. My proposal above does all of these.
For your problem, you may need two different regexes, one for either case.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Regex to identify unique words in a dictionary database gimley Shell Programming and Scripting 2 02-21-2017 03:09 AM
Identify file pattern, take count of pattern, then act ampsys Shell Programming and Scripting 3 05-13-2015 05:34 AM
Regex to identify word in second position on a line gimley Shell Programming and Scripting 8 04-21-2013 11:07 PM
Regex to identify a full-stop as a sentence delimiter gimley Shell Programming and Scripting 9 07-30-2012 09:46 AM
Use Regex to identify / format a complex string egill UNIX for Dummies Questions & Answers 1 06-04-2012 06:52 PM



All times are GMT -4. The time now is 03:44 AM.