I am learning regular expression in sed,Please help me understand the use curly bracket in sed,


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting I am learning regular expression in sed,Please help me understand the use curly bracket in sed,
# 1  
Old 06-04-2014
Debian I am learning regular expression in sed,Please help me understand the use curly bracket in sed,

I am learning SED and just following the shell scripting book, i have trouble understanding the grep and sed statement,

Question : 1
__________
Code:
/opt/oracle/work/antony>cat teledir.txt
jai sharma 25853670
chanchal singhvi 9831545629
anil aggarwal 9830263298
shyam saksena 23217847
lalit chowdury 26688726


[quint2]PCID
/opt/oracle/work/antony> grep '[0-9]\{10\}' teledir.txt 
chanchal singhvi 9831545629
anil aggarwal 9830263298

To my understanding , the above select will look for the number between 0 to 9 which should no exceed character 10.

If my above statement correct, then i should get the 8 digit mobile number in below select statement, but i am getting 10 and 8 digit number. it suppose to look for 0 to 9 and give 8 digit mobile number. please correct me if i am wrong.

Code:
/opt/oracle/work/antony>  grep '[0-9]\{8\}' teledir.txt
jai sharma 25853670
chanchal singhvi 9831545629
anil aggarwal 9830263298
shyam saksena 23217847
lalit chowdury 26688726

Question 2
-----------

Code:
/opt/oracle/work/antony>  ls -l | sed -n '/^.\{2,3\}w/p'
-rw-r-----   1 oracle     dba             11 Jun  2 15:28 control.sql
-rw-r-----   1 oracle     dba             50 Mar 24 16:40 db_list
-rw-r-----   1 oracle     dba            386 Jun  3 16:34 emp.1st
-rw-r-----   1 oracle     dba              0 Jun  3 16:39 sed
-rw-r-----   1 oracle     dba            327 Jun  2 15:09 stuff.sql
-rw-r-----   1 oracle     dba            120 Jun  3 20:10 teledir.txt
drwxr-x---   2 oracle     dba             96 Jun  3 17:14 test
-rwxr-xr-x   1 oracle     dba            330 Mar 24 16:42 trex.sh

From the above select statement,

1. i am not able to understand what is the use of curly brackets?
2. \{2,3\}w/p' - what does each argument meant for ?
3. what is the use of w,{2,3\}?

Please explain

Thanks

Antony Ankrose J

Last edited by Antony Ankrose; 06-04-2014 at 04:03 PM.. Reason: Please use [code][/code] tags.
# 2  
Old 06-04-2014
Quote:
Originally Posted by ANTONY ANKROSE
To my understanding , the above select will look for the number between 0 to 9 which should no exceed character 10.
It will look for exactly 10 digits in a row.

These exact 10 digits can appear anywhere in the line.

Quote:
If my above statement correct, then i should get the 8 digit mobile number in below select statement, but i am getting 10 and 8 digit number. it suppose to look for 0 to 9 and give 8 digit mobile number. please correct me if i am wrong.
It will find 8 digits inside 10 digits, the same way it will find 'gas' inside 'gasoline'.

To find only 8 digits you must be more specific. Perhaps

Code:
grep ' [0-9]{8}$'

to tell it there must be a space in front, and that the string must end at the end of the line.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 06-04-2014
A snippet from man regexp
Code:
Matching a specified number of occurrences
    A BRE interval expression has the syntax ``\{l\}'', ``\{l,\}'',
   or ``\{l,u\}''.
    An ERE interval expression has the syntax ``{l}'', ``{l,}'', or
   ``{l,u}''.

Also,
I found this site:
http://http://txt2re.com/index-python.php3
very helpful when dealing with regular expressions
# 4  
Old 06-04-2014
Thanks you very much , i got it clarified.

Please help me to understand sed statement

Question 2
-----------


Code:
Code:
/opt/oracle/work/antony>  ls -l | sed -n '/^.\{2,3\}w/p'
-rw-r-----   1 oracle     dba             11 Jun  2 15:28 control.sql
-rw-r-----   1 oracle     dba             50 Mar 24 16:40 db_list
-rw-r-----   1 oracle     dba            386 Jun  3 16:34 emp.1st
-rw-r-----   1 oracle     dba              0 Jun  3 16:39 sed
-rw-r-----   1 oracle     dba            327 Jun  2 15:09 stuff.sql
-rw-r-----   1 oracle     dba            120 Jun  3 20:10 teledir.txt
drwxr-x---   2 oracle     dba             96 Jun  3 17:14 test
-rwxr-xr-x   1 oracle     dba            330 Mar 24 16:42 trex.sh

From the above select statement,

1. i am not able to understand what is the use of curly brackets?
2. what is the use of {2,3\}w meant for?

Please explain

Last edited by Antony Ankrose; 06-04-2014 at 05:23 PM.. Reason: CODE tags added
# 5  
Old 06-04-2014
Starting at the beginning of the line i.e. ^, match either 2 or 3 of any character, i.e. "." before a "w" character.
# 6  
Old 06-04-2014
Thanks Corona688

Still i am not able to get you completely what you say
^ Matches the beginning of lines.

. Matches any single character.

Which means the command matches any character in the beginning of the line followed by any single character and then followed by checking any character between 2 or 3
in the first column and not able to get what this w for ?

Please help me understand
# 7  
Old 06-04-2014
Quote:
Originally Posted by ANTONY ANKROSE
Thanks Corona688

Still i am not able to get you completely what you say
^ Matches the beginning of lines.

. Matches any single character.

Which means the command matches any character in the beginning of the line followed by any single character and then followed by checking any character between 2 or 3
No, it matches 2 or 3 of "any character". It'd match AB, QRZ, --, or anything that's two or three characters long.

So basically, this regex means "the third or fourth character must be a w".

Quote:
not able to get what this w for?
The letter "w" in the regex matches the letter "w" in the string.

Code:
ls -l | sed -n '/^.\{2,3\}w/p'
...
-rwxr-xr-x   1 oracle     dba            330 Mar 24 16:42 trex.sh
...

This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed regular expression

Hi , I need to remove pipe character from a |^ delimeted file. Something like |^tran|sformers||^|revenge |of fallen|^ to |^transformers|^revenge of fallen|^... Cold anybody please help to build the regular expression using sed . many thanks. Please use code tags next time for... (1 Reply)
Discussion started by: kokjek
1 Replies

2. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

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)
Discussion started by: InduInduIndu
11 Replies

3. Shell Programming and Scripting

Help with sed regular expression

Hi all, I want to get a substring from a string based on given delimiter, for example: str="foo|bar|baz" with delimiter "|", I want to get one substring at each time with the order number the substring in the whole string, given 1 to get "foo", given 2 to get "bar", given 3 to get "baz", I... (2 Replies)
Discussion started by: Roy987
2 Replies

4. UNIX for Dummies Questions & Answers

Regular Expression In Sed

Hi , I am learing sed echo abc 123 def 456 | sed 's|\(*\) \(*\)|\1|' is returning abc def 456 i was hoping abc def "\1" should only print the occurence of the first pattern but according to my understanding it is just removing the first occurence of the second pattern... (7 Replies)
Discussion started by: max_hammer
7 Replies

5. Shell Programming and Scripting

sed regular expression help

please consider this: echo "11111*X*005010X279~ST*270*1111111*005010X279~BHT*0011*11" | sed 's/.*\(005010X(\d)(\d)(\d)*\).*$/\1/'i'm searching for first occurrence of 005010X while leaving rest of characters out. :confused: any tips? thnx in advance guys. (7 Replies)
Discussion started by: grep01
7 Replies

6. Shell Programming and Scripting

Regular expression (sed)

Hi I need to get text that are within "" For example File: asdasd "test test2" sadasds asdda asdasd "demo demo2" Output: test test2 demo demo2 Any help is good Thank you (12 Replies)
Discussion started by: blito_loco
12 Replies

7. Shell Programming and Scripting

SED (regular expression) problem ---

Hello, I would like to replace Line 187 of my file named run_example. The original line is below, including the spaces: celldm(1) = 6.00, I want it to become something like celldm(1) = 6.05, or celldm(1) = 6.10, where the number is stored in a variable called... (6 Replies)
Discussion started by: bluesmodular
6 Replies

8. Shell Programming and Scripting

Regular expression with SED

Hi! I'm trying to write a regexp but I have no luck... I have a string like this: param1=sometext&param2=hello&param3=bye Also, the string can be simply: param2=hello I want to return the value of param2: "hello". How can I do this? Thanks. (3 Replies)
Discussion started by: GagleKas
3 Replies

9. Shell Programming and Scripting

Regular expression with sed

Hi, I'm trying following:echo "test line XA24433 test" | sed 's/.*X\(.*\)/X\1/' XA24433 test While I want the output as: XA24433 I want to grab the words starting with letter X till the next space, this word can be anywhere in the line. (9 Replies)
Discussion started by: nervous
9 Replies
Login or Register to Ask a Question