Regex - Capturing groups


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regex - Capturing groups
# 8  
Old 11-13-2013
Code:
perl -ne 'print "$1 $2\n" if /(\w+)\s(?:.*)\((.*)\);$/' inputfile

Code:
-n is similar to while loop without printing the lines
while (<>) {
# do something
}

Code:
-e - perl code written in command line

Code:
\w - matches word ie alphanumeric and underscore 
\s - matches whitespace or tab
(\w+) - used for grouping - $1 as a backreference
(?: ) - grouping but will not make backreference. Hence it will not be $2.
\( - matches literal (
(.*) - grouping ie matches any character - $2 as a backreference
\) - matches literal )

# 9  
Old 11-13-2013
Hello,

one more approach, may help. Let us say file name is check_column_bracket.


Code:
awk '{
n=split($NF,array,"[(.*]");
print $1" "array[n]}' check_column_bracket | awk -vs=");" 'gsub(s,X)'

Output wil be as follows.

Code:
ABC XYZ,DEF,GHI
XYZ ABC
BHI LHJ



Thanks,
R. Singh
# 10  
Old 11-13-2013
Quote:
Originally Posted by RavinderSingh13
Output wil be as follows.
Code:
ABC XYZ,DEF,GHI
XYZ ABC
BHI LHJ

But OP's desired output is:
Code:
ABC XYZ,DEF,GHI
XYZ ABC
BHI  OWE,RTY,LHJ

By the way it seems to me that OP is not interested in an awk/shell solution, but satisfied with a perl based solution.
# 11  
Old 03-21-2014
Hello,

Here is a solution which have same output as per request.

Code:
awk '{match($0,/\(.*\)/); check=substr($0,RSTART+1,RLENGTH-2); gsub(/[[:space:]]/,X,check); {print $1 OFS check}}'  file_name


Output is as follows:

Code:
ABC XYZ,DEF,GHI
XYZ ABC
BHI QWE,RTY,LHJ


Thanks,
R. Singh
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sendmail K command regex: adding exclusion/negative lookahead to regex -a@MATCH

I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works: LOCAL_CONFIG # Kcheckaddress regex -a@MATCH +<@+?\.++?\.(us|info|to|br|bid|cn|ru) LOCAL_RULESETS SLocal_check_mail # check address against various regex... (0 Replies)
Discussion started by: RobbieTheK
0 Replies

2. Shell Programming and Scripting

Perl, RegEx - Help me to understand the regex!

I am not a big expert in regex and have just little understanding of that language. Could you help me to understand the regular Perl expression: ^(?!if\b|else\b|while\b|)(?:+?\s+){1,6}(+\s*)\(*\) *?(?:^*;?+){0,10}\{ ------ This is regex to select functions from a C/C++ source and defined in... (2 Replies)
Discussion started by: alex_5161
2 Replies

3. UNIX for Dummies Questions & Answers

read regex from ID file, print regex and line below from source file

I have a file of protein sequences with headers (my source file). Based on a list of IDs (which are included in some of the headers), I'd like to print out only the specified sequences, with only the ID as header. In other words, I'd like to search source.txt for the terms in IDs.txt, and print... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

4. Shell Programming and Scripting

Perl newbie - regex replace all groups issue

Hello, Although I have found similar questions, I could not find advice that could help with my problem. The issue: I am trying to replace all occurrences of a regex, but I cannot make the regex groups work together. This is a simple input test file: The Vedanta Philosophy... (3 Replies)
Discussion started by: samask
3 Replies

5. Shell Programming and Scripting

Need help with regex groups

I have a requirement - replace specified positions in a string with a character. I found perl regex useful for this approach. however, I am facing the following issue. The target file 'temp' contains - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx The goal is to convert... (5 Replies)
Discussion started by: sam_roy
5 Replies

6. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

7. Shell Programming and Scripting

date capturing regex and storing

Hi all I need help on how to store two or more date formates captured using regex from an input sentence in PERL ? For example, I have an input sentence consisting of two dates such as : The departure date is August 12, 2009 and arrival date is 20.08.2009. Now, I want to capture the two... (4 Replies)
Discussion started by: my_Perl
4 Replies

8. Shell Programming and Scripting

Capturing regex of perl

Hi all I am struggling to find out the capturing regex of a date format such as 10/12/2009. Also I need help on how to assign the date(i.e, 10/12/2009 ) to a variable after the match is found using the capturing regex. Any help is appreciated. Thanks in advance. (5 Replies)
Discussion started by: my_Perl
5 Replies

9. Shell Programming and Scripting

Grep regex matches, groups

Hello, I am searching all over the place for this, just not finding anything solid :( I want to do be able to access the groups that are matched with grep (either with extended regex, or perl compatible regex). For instance: echo "abcd" | egrep "a(b(c(d)))" Of course this returns... (1 Reply)
Discussion started by: Rhije
1 Replies

10. Solaris

groups

1 user in member of 4 groups find file permissions and default group (1 Reply)
Discussion started by: tirupathi
1 Replies
Login or Register to Ask a Question