Curly braces in sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Curly braces in sed
# 1  
Old 06-18-2015
Curly braces in sed

Hi,

I have below command in one of the script. Can you please let me know what does the curly braces do over here \{1,\}. The remaining part of the code atleast I am able to understand.

Code:
sed -n 's/.*\-\([0-9])\{1,\}\)\-.*/\1/p'

# 2  
Old 06-18-2015
Hi, it means 1 or more occurrences of the preceding character or sub-expression, in this case: ), so 1 or more closing parentheses..
# 3  
Old 06-18-2015
Quote:
Originally Posted by tostay2003
Can you please let me know what does the curly braces do over here \{1,\}.
Scrutinizer is right. You can use this device to "multiply" a previous expression, similar to a "*", but with added functionality. For instance:

Code:
X            # matches exactly one single "X"
X*           # matches any number of "X"s, including zero
X\{3\}       # matches exactly 3 "X"s
X\{1,\}      # matches any number of "X"s, from 1 up
X\{,5\}      # matches up to 5 "X"s
X\{3,5\}     # matches 3 to 5 "X"s, hence either "XXX", "XXXX" or "XXXXX"

Notice, that, instead of a single character like "X" here, you can also modify complex expressions with that modifier. For instance:

Code:
|[^|]*          # matches a "field" in tabular, pipe-separated data
                # i.e. "|field1|field2|field3....."
 
\(|[^|]*\)\{3\} # matches 3 such fields

I hope this helps.

bakunin
# 4  
Old 06-18-2015
Code:
sed -n 's/.*\.\([0-9]\{1,\}\)\..*/\1/p'

I had it slightly wrong, but I got the idea of \{1,\}

However when I have the value such as

Code:
echo "testing.123.xyz.456.txt" | sed -n 's/.*\.\([0-9]\{1,\}\)\..*/\1/p'

I am getting value "456" instead of first pattern. Is there anywhere that I am going wrong.
# 5  
Old 06-18-2015
Code:
echo "testing.123.xyz.456.txt" | sed -n 's/.*\.\([0-9]\{1,\}\)\..*/\1/p'

The issue is that the red part of the regex matches the part of the string. The `.*' will try to match as much as it can.

Perhaps, modifying your regex a bit:
Code:
echo "testing.123.xyz.456.txt" | sed -n 's/[^.]*\.\([0-9]\{1,\}\)\..*/\1/p'

[^.]*: keep matching any char that is not a literal period. Stops when it does.
This User Gave Thanks to Aia For This Post:
# 6  
Old 06-18-2015
Or to get the first field with numbers, try:
Code:
sed -n 's/^\([^0-9.].\)*\([0-9]*\).*/\2/p'

or if is always the second field, try:
Code:
cut -d. -f2


Last edited by Scrutinizer; 06-18-2015 at 09:58 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 06-18-2015
Quote:
Originally Posted by Aia
Code:
echo "testing.123.xyz.456.txt" | sed -n 's/.*\.\([0-9]\{1,\}\)\..*/\1/p'

The issue is that the red part of the regex matches the part of the string. The `.*' will try to match as much as it can.

Perhaps, modifying your regex a bit:
Code:
echo "testing.123.xyz.456.txt" | sed -n 's/[^.]*\.\([0-9]\{1,\}\)\..*/\1/p'

[^.]*: keep matching any char that is not a literal period. Stops when it does.
Great.. This works... is ^ in square brackets used as a negation?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check string end with curly braces

file.txt apple apples{ applepicture apple9 apple cake{ abple apple_and_cake appleapple apple apple( and my script while read line; do if ]; then echo "$line" fi done <file.txt read (10 Replies)
Discussion started by: cmdcmd
10 Replies

2. Shell Programming and Scripting

When curly braces needed?

Hello, i was trying to find get a command to list duplicated files so i tried ls dir1 dir2 | awk '{x++}' and it didnt work. After a bit of searching online i found that it works without the curly braces ls dir1 dir2 | awk 'x++' I thought the curly braces were needed in awk so... (6 Replies)
Discussion started by: andy391791
6 Replies

3. Shell Programming and Scripting

How to remove newline, tab, spaces in curly braces.. :( Pls Help?

Hi Everyone, in the below "xyz (Exception e)" part... after the curly braces, there is a new line and immediately few tabs are present before closing curly brace. xyz (Exception e) { } note: there can be one or more newlines between the curly braces. My desired output should be ... (6 Replies)
Discussion started by: NY_777
6 Replies

4. Shell Programming and Scripting

** EMERGENCY ** Having trouble with curly braces.. :( Pls Help

Hi Everyone, in the below "xyz (Exception e)" part... after the curly braces, there is a new line and immediately few tabs are present before closing curly brace. xyz (Exception e) { } note: there can be one or... (1 Reply)
Discussion started by: NY_777
1 Replies

5. UNIX for Dummies Questions & Answers

How do I pull the value between curly braces?

Hi everyone, I've got a file that looks like this: uid{508}pid{22224}pname{/PPROGRAM/pprgramx -profile:LIVE -serv:as ... I want to pull the value of pid between the curly braces, or 22224 in this example. pid is always the second pair of curly braces, but the length of the number is... (7 Replies)
Discussion started by: Scottie1954
7 Replies

6. Shell Programming and Scripting

tar --exclude with curly braces

I'm having trouble understanding the exclude option in tar. From some web sites, it seems one is able to exclude several strings by enclosing them in curly brackets. However it seems to be "random" what gets excluded when using the curlies. I've been using the exclude-from=myfile option in a... (12 Replies)
Discussion started by: majest
12 Replies

7. Shell Programming and Scripting

sed in windows does not parse curly braces

Hi everyone: I'm stuck at this point, could you guys please give me some hints about what I am doing wrong in the following script, I'm using sed for windows: sed ^"$ {^ a^ STRINGTABLE DISCARDABLE^ BEGIN^ #define CLIENT_MODULE, "%CLIENT_MODULE%"^ #define CLIENT_ID, "%CLIENT_ID%"^... (1 Reply)
Discussion started by: edgarvm
1 Replies

8. Shell Programming and Scripting

find -regex not recognizing curly braces

Must be a bug or something. Whether I escape them or not, it will not work. No matter what I set the minimum and maximum to nothing gets caught. For instance: find / -regex "/.{0, 50}.*" -maxdepth 1 or find / -regex "/.\{0, 50\}.*" -maxdepth 1 should pretty much catch everything residing within... (4 Replies)
Discussion started by: stevensw
4 Replies

9. Shell Programming and Scripting

Curly braces assigned to variables

Hi, Im pretty new to Unix. I came across a script which was using PLSQL inside a script and there was an unusual thing mentioned. there was a variable assigned as P_CUR=${1} and one more as V_TAGFILE="$1" Couldnt find the difference. Also the variables were used in PLSQL... (1 Reply)
Discussion started by: njks68
1 Replies

10. Shell Programming and Scripting

Use of curly braces with variables

Hi, I am new to shell scripting.I have worked somewhat with Perl though. I am not able to find what the second line does and how does it do. <code> FP_RUNNING=`service filepool status` FP_RUNNING=${FP_RUNNING%% *} <\code> After the first line,the variable FP_RUNNING stores '1 FilePool... (2 Replies)
Discussion started by: abhinavsinha
2 Replies
Login or Register to Ask a Question