Visit Our UNIX and Linux User Community


regular expressions memory


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users regular expressions memory
# 1  
Old 07-25-2009
regular expressions memory

Hi all, i have read in one site (i can put link, but is in Czech language ) that regular expressions have some kind of memory. If i understood it propertly when i want to remebmer some part of regexp i just put it between \( and ) and then call it by \1 \2 \3 etc. where \$number is number of memored expression. So i tried some examples bud didnt work for me :

lets say i want to know only UID and HOME from /etc/passwd so i tried followings:

Code:
grep -E "^[^:]*:[^:]*:\([^:]*):[^:]*:[^:]*:\([^:]*):[^:]*" /etc/passwd
grep -E "^[^:]*:[^:]*:\([^:])*:[^:]*:[^:]*:\([^:])*:[^:]*" /etc/passwd

but neither one works. I know i didnt call \1 and \2 but i dont know how.

also tried append "end" string to end of all lines of file

Code:
sed -e 's/\(.*)/\1end/' file_name

with the same result as prevouis.

I know both tasks can be done using some other utils such as sed. But just for curiosity i want know more about regexp memory. Thanks a lot

---------- Post updated at 05:37 PM ---------- Previous update was at 05:32 PM ----------

Noticed that the block of text that to be memored must be in \(....\), works in sed, in grep still dont know how to print just \1 \2
# 2  
Old 07-25-2009
grep will always output the whole line, no matter how small the part you actually searched for.

That said, the "memory" you're referring to are called "capturing groups", and can even be nested. If, for example, you take the Perl expression
Code:
$string = "This is a test.";
$string =~ /((\w+)\s(\w+))$/;

You'd have "a test." in $1, "a" in $2, and "test." in $3
# 3  
Old 07-25-2009
There is a typo error in your code.
It should be:
Code:
sed -e 's/\(.*\)/\1end/' file_name

Actually the below will work:
Code:
sed -e 's/.*/& end/' file_name

# 4  
Old 07-28-2009
Yes you re right capture must be \(regexp\)
# 5  
Old 07-28-2009
You say you want the uid and home directory of all users?
Try awk:
awk 'BEGIN { FS=":"; OFS="\ "; } { print $1, $6; }' /etc/passwd
# 6  
Old 07-29-2009
The more formal term is backreference. A backreference stores the part of the string matched by the part of the regular expression inside the parentheses.

Previous Thread | Next Thread
Test Your Knowledge in Computers #287
Difficulty: Medium
AIX 7.2 was initially released on December 1, 2013.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regular expressions

I need to pick a part of string lets stay started with specific character and end with specific character to replace using sed command the line is like this:my audio book 71-skhdfon1dufgjhgf8.wav' I want to move the characters beginning with - end before. I have different files with random... (2 Replies)
Discussion started by: XP_2600
2 Replies

2. Shell Programming and Scripting

Help with regular expressions

I have a file that I'm trying to find all the cases of phone number extensions and deleting them. So input file looks like: abc x93825 def 13234 x52673 hello output looks like: abc def 13234 hello Basically delete lines that have 5 numbers following "x". I tried: x\(4) but it... (7 Replies)
Discussion started by: pxalpine
7 Replies

3. Shell Programming and Scripting

Regular Expressions

what elements does " /^/ " match? I did the test which indicates that it matches single lowercase character like 'a','b' etc. and '1','2' etc. But I really confused with that. Because, "/^abc/" matches strings like "abcedf" or "abcddddee". So, what does caret ^ really mean? Any response... (2 Replies)
Discussion started by: DavidHe
2 Replies

4. Shell Programming and Scripting

Regular Expressions

#!/usr/bin/perl $word = "one last challenge"; if ( $word =~ /^(\w+).*\s(\w+)$/ ) { print "$1"; print "\n"; print "$2"; } The output shows that "$1" is with result one and "$2" is with result challenge. I am confused about how this pattern match expression works step by step. I... (8 Replies)
Discussion started by: DavidHe
8 Replies

5. UNIX for Dummies Questions & Answers

Regular expressions

In regular expressions with grep(or egrep), ^ works if we want something in starting of line..but what if we write ^^^ or ^ for pattern matching??..Hope u all r familiar with regular expressions for pattern matching.. (1 Reply)
Discussion started by: aadi_uni
1 Replies

6. UNIX for Advanced & Expert Users

regular expressions

I have a flat file with the following drug names Nutropin AQ 20mg PEN Cart 2ml Norditropin Cart 15mg/1.5ml I have to extract digits that are before mg i.e 20 and 15 ; how to do this using regular expressions Thanks ram (1 Reply)
Discussion started by: ramky79
1 Replies

7. UNIX for Dummies Questions & Answers

regular expressions

how to find for a file whose name has all characters in uppercase after 'project'? I tried this: find . -name 'project**.pdf' ./projectABC.pdf ./projectABC123.pdf I want only ./projectABC.pdf What is the regular expression that correponds to "all characters are capital"? thanks (8 Replies)
Discussion started by: melanie_pfefer
8 Replies

8. Shell Programming and Scripting

regular expressions

Hi, can anyone advise me how to shorten this: if || ; then I tried but it dosent seem to work, whats the correct way. Cheers (4 Replies)
Discussion started by: jack1981
4 Replies

9. Shell Programming and Scripting

Regular Expressions

How can i create a regular expression which can detect a new line charcter followed by a special character say * and replace these both by a string of zero length? Eg: Input File san.txt hello hi ... (6 Replies)
Discussion started by: sandeep_hi
6 Replies

10. Programming

regular expressions in c++

How do I use the regular expressions in c++? (2 Replies)
Discussion started by: szzz
2 Replies

Featured Tech Videos