LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 07-25-2009, 09:52 AM   #1
wakatana
Member
 
Registered: Jul 2009
Location: Slovakia
Posts: 133

Rep: Reputation: 16
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
 
Old 07-25-2009, 10:09 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
1) The syntax to remember part of the regexp is \(...\), that is you have to escape both the parentheses. I don't know how to use it with grep anyway, or even if it's possible. I thought it was a sed feature.

2) to append a string at the end of each lines using sed:
Code:
sed 's/$/end/g' file
3) to extract info from the /etc/passwd file you can try awk using a colon as field separator:
Code:
awk -F: '{print $6}' /etc/passwd
 
Old 07-25-2009, 10:21 AM   #3
wakatana
Member
 
Registered: Jul 2009
Location: Slovakia
Posts: 133

Original Poster
Rep: Reputation: 16
Tried it with \(...\) in sed, so following command works same as previous
Code:
sed -e 's/\(.*\)/\1end/g' filename
thanks


in grep, now accept command by bash, but still dont know how to prnt only \1 and \2. Maybe youre right and in grep is not possible
Code:
grep "^[^:]*:\([^:]*\):[^:]*:[^:]*:[^:]*:\([^:]*\):[^:]*" passwd
PS: yes i know awk can do that, but i want learn more about this feature. tkanks anyway

Last edited by wakatana; 07-25-2009 at 10:31 AM.
 
Old 07-25-2009, 11:41 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Quote:
Originally Posted by wakatana View Post
in grep, now accept command by bash, but still dont know how to prnt only \1 and \2. Maybe youre right and in grep is not possible
grep has the -o option to print only the part of a line that matches the regular expression. An example of "regexp memory" usage not strictly related to sed is here.
 
Old 07-26-2009, 07:42 AM   #5
wakatana
Member
 
Registered: Jul 2009
Location: Slovakia
Posts: 133

Original Poster
Rep: Reputation: 16
Thanks for reply, I understand that, with sed i can do exactly what i was looking for, just enclose part of regexp into \(...\) and and then "call" it by \number. BTW grep -o is also interesting, thanks
 
Old 07-27-2009, 12:03 AM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
Here's a good sed manual http://www.grymoire.com/Unix/Sed.html
Regexes http://www.grymoire.com/Unix/Regular.html
 
Old 10-13-2009, 07:54 AM   #7
wakatana
Member
 
Registered: Jul 2009
Location: Slovakia
Posts: 133

Original Poster
Rep: Reputation: 16
thanks , sorry for long response
 
  


Reply

Tags
expressions, memory, regexp, regular


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expressions ziggy25 Linux - Newbie 7 11-05-2007 06:57 AM
Regular expressions bhuwan Programming 5 02-25-2006 11:07 PM
Regular Expressions markjuggles Programming 2 05-05-2005 11:39 AM
Regular Expressions overbored Linux - Software 3 06-24-2004 02:34 PM
help with REGULAR EXPRESSIONS ner Linux - General 23 10-31-2003 11:09 PM


All times are GMT -5. The time now is 08:34 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration