I read the book of <<unix shell programming>>. The regular expression ^\(.\)\1 matches the first character on the line and stores it in register 1. Then the expression matches whatever is stored in the register 1, as specified by the \1. The net effect of this regular expression is to match the first two characters on a line if they are both the same character.
I don't fully understand this regular expression, especially "Then the expression matches whatever is stored in the register 1, as specified by the \1." Can someone explain it in detail and more clearly.
If you surround a block within a regular expression with escaped parenthesis (or un-escaped parenthesis when using Perl compatible regex eg. egrep) you are asking the regex parser to remember what was just matched and store it as the next back reference. Thus
Code:
grep '^\(.\)\1' file
will match any character at the start of a line (the . character)
and store it in the first back reference which can be addressed as \1.
a more obvious example might be where we had a file with records of the form
user homeNode
and we wished to create an internal mailing list
Code:
sed s'/^([^ ]+) ([^ ]+)$/\1@\2/' users_file.txt
This would print out a series of email addresses
Then again re-reading that I'm not sure if it helps, the following may show it more clearly, assuming you've looked at alternation
Another example would be if you wished to match all of a html tag that could contain a ">" character in it (eg. <img alt="Next>" src="/images/next_button.gif"/>)
Code:
<([^>]+|(["'])[^\2]+\2)+>
Here we match anything that is not a ">" character, or anything that is a quoted string which uses either single or double quotes. We capture the quote type in the second set of parenthesis, (the first being the alternation), and then keep matching characters until the end of the quoted string is marked by the quote we previously matched.
Last edited by Skrynesaver; 06-21-2011 at 08:42 AM..
Reason: Second simpler? example
hi All ,
I am having a large file with lots of modules as shown below
###############################################
module KKK
kksd
kskks
jsn;lsm
jsnlsn;
Ring
jjsjsj
kskmsm
jjs
endmodule
module llll
1kksd11
k232skks
j33sn;l55sm (6 Replies)
I had a string in perl script as below.
Tue Augáá7 03:54:12 2012
Now I need to replace the special character with space.
After removing the special chaacters
Tue Aug 7 03:54:12 2012
Could anyone please help me here for writing the regular expression?
Thanks in advance..
Regards,
GS (1 Reply)
I thought this would be easy to Google, but I am having trouble getting a clean result that I can understand.
I simply want to insert the the line:
My Network 192.168.1.1
After the last line that begins with ACL localnet (15 Replies)
Hello All,
I'm trying to extract the lines between two consecutive elements of an array from a file.
My array looks like:
problem_arr=(PRS111 PRS213 PRS234)
j=0
while } ]
do
k=`expr $j + 1`
sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt
---some operation goes... (11 Replies)
Hi all,
How am I read a file, find the match regular expression and overwrite to the same files.
open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat";
open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat";
while (<DESTINATION_FILE>)
{
# print... (1 Reply)
Hello:
(exp) : match "exp",the matched text is stored in auto named arrays.
How can I get the matched text ? What is the name of the auto named arrays on linux shell ? (4 Replies)
I am completely new to perl programming. My father is helping me learn said programming language. However, I am stuck on one of the assignments he has given me, and I can't find very much help with it via google, either because I have a tiny attention span, or because I can be very very dense.
... (4 Replies)
CA_RELEASE has a value of 6. I need to check if that this is a numeric value. if not error.
source $CA_VERSION_DATA
if * ]
then
echo "CA_RELESE $CA_RELEASE is invalid"
exit -1
fi
+ source /etc/ncgl/ca_version_data
++ CA_PRODUCT_ID=samxts
++ CA_RELEASE=6
++ CA_WEEK_NO=7
++... (3 Replies)
Hello All
I have file which contain sample data like below -
test.txt
----------------------------------------------
jambesh aaa india
trxxx
sdasd
mentor
asss
light
train
bbblah
---------------------------------------------
I want to write a regX which would print only those... (4 Replies)
Is it possible to combine a regular expression with a aritmetical expression? For example, taking a 8-numbers caracter sequece and casting each output of a grep, comparing to a constant.
THX! (2 Replies)