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:

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

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

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

$string = "This is a test.";
$string =~ /((\w+)\s(\w+))$/;

You'd have "a test." in $1, "a" in $2, and "test." in $3

There is a typo error in your code.
It should be:

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

Actually the below will work:

sed -e 's/.*/& end/' file_name

Yes you re right capture must be \(regexp\)

You say you want the uid and home directory of all users?
Try awk:
awk 'BEGIN { FS=":"; OFS="\ "; } { print $1, $6; }' /etc/passwd

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.