Curly braces in sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Curly braces in sed
# 8  
Old 06-18-2015
Quote:
Originally Posted by tostay2003
Great.. This works... is ^ in square brackets used as a negation?
Exactly:

Code:
[AB]     # matches "A" or "B"
[^AB]    # matches any character except "A" or "B"

You need this quite often, because matches in sed are always "greedy". Suppose the following text:

Code:
AXXBABABABXABABABAB

the expression /A.*B/ will match the whole string, not just the first four characters! If several possiblities for a match exist always the longest possible one will be taken:
Code:
AXXBABABABXABABABAB
A<------ .* ----->B

If you want to match only up to the first "B" you need to:

Code:
AXXBABABABXABABABAB
A[^B]*B
AXXB

I hope this helps.

bakunin

Last edited by bakunin; 06-18-2015 at 11:23 AM..
This User Gave Thanks to bakunin For This Post:
# 9  
Old 06-24-2015
Hi,


Thanks for the detailed description.

I tried changing the below sed to fetch the first available numeric, but was unable to get the results.

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

Ideally, my code has to fetch any numeric value that comes first and matching a pattern.

Code:
Pattern -> testing.123.xyz.*.txt 
Actual Value -> testing.123.xyz.456.txt 
----> This should result in 123

If pattern DOES NOT have a * only first available value needs to be extracted
Code:
abc123testing456 ----> should result in 123
123abctesting456 ----> should result in 123

Any ideas how we can change the code to achieve the above result

Last edited by tostay2003; 06-24-2015 at 08:46 AM..
# 10  
Old 06-24-2015
Not quite sure what you mean by "above result". However, try
Code:
sed -rn 's/[^0-9]*([0-9]{1,})[^0-9]*.*/\1/p' file

This User Gave Thanks to RudiC For This Post:
# 11  
Old 06-24-2015
Quote:
Originally Posted by RudiC
Not quite sure what you mean by "above result". However, try
Code:
sed -rn 's/[^0-9]*([0-9]{1,})[^0-9]*.*/\1/p' file

In all the below cases (for eg.) the result should be "1234"
Code:
abc.1234.xyz.456.999
abc.1234testing456 
abc1234testing456 
1234abctesting456

Code:
Result :1234

I do not have system in front of me. I can check it tomorrow.

Does the above code which you have given return
Code:
"23"

. Please correct me If I am wrong whether the values 1 and 4 gets absorbed by
Code:
[^0-9]

# 12  
Old 06-24-2015
Result of sed script in post#10 applied to your last sample:
Code:
1234
1234
1234
1234


Last edited by RudiC; 06-25-2015 at 10:48 AM..
# 13  
Old 06-24-2015
Code:
$ cat numbers.file
abc.1234.xyz.456.999
abc.1234testing456 
nonumbershere
abc1234testing456 
voidofnumbers
1234abctesting456

$ perl -nle '/(\d+)/ and print $1' numbers.file 
1234
1234
1234
1234

# 14  
Old 06-25-2015
Regular sed:
Code:
sed -n 's/^[^0-9]*\([0-9]\{1,\}\).*/\1/p' file

GNU awk:
Code:
awk -v FPAT='[0-9]+' 'NF{print $1}' file

Regular awk:
Code:
awk 'match($0,/[0-9]+/){print substr($0,RSTART,RLENGTH)}' file


Last edited by Scrutinizer; 06-25-2015 at 10:17 AM..
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