Sed regex problem


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Sed regex problem
# 8  
Old 11-11-2010
Quote:
Originally Posted by ctsgnb
you could more simply use
Code:
date | sed 's/^.*\(..:..:..\).*$/\1/'

You could even leave out the start/end markers:
Code:
sed 's/.*\(..:..:..\).*/\1/'

Since the : are sufficient as anchors...
# 9  
Old 11-11-2010
Yeah, sure there are plenty of ways to get the time from date. My problem was that I did not know that sed treads + and { primarily as a normal character and not as a regex.

Maybe we should start an offtopic thread with all ways to extract the time from `date` with *nix like systems or even all kind of OS Smilie
# 10  
Old 11-12-2010
First of all welcome to this forum.

There was another, more important problem with your sed attempts and that was the greediness of sed, as ctsgnb pointed out. My post was related to that and made a point about anchoring the regex. It was perhaps primarily meant for ctsgnb, but it may be of use to you too, so I reckon it certainly is related to this thread and it is not off topic. Please also note that this is not "your" thread as the OP. A thread may be of use to you as the OP, to those who participate, to anyone who reads along and to those who use it as a reference or land there as the result of a query.

--

Further, it is not so much that sed treats + and {} as normal characters. It is that modern sed adheres to POSIX BRE (Basic Regular Expressions) and as such curly brackets need to be escaped, but + is not even supported. Only GNU sed has an extension that supports it as \+ .

If you wish to use ERE (Extended Regular Expressions) you can use GNU sed with the -r option and then + and {} can be used without the escape, since { and + are part of ERE.

Last edited by Scrutinizer; 11-12-2010 at 03:00 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regex with sed

hi i would like to say "DATABASENAME=" to "TABLESNAME=" remove "," and press enter myconfig file thanks (1 Reply)
Discussion started by: mnnn
1 Replies

2. Shell Programming and Scripting

Multiple regex in sed

I am using the following sed script to remove new lines (\r\n and \n), except from lines starting with >: sed -i ':a /^>/!N;s/\r\n\(\)/\1/;s/\n\(\)/\1/;ta' Is there a way to include both \r\n and \n in one regex to avoid the second substitute script (s/\n\(\)/\1/)? (4 Replies)
Discussion started by: Xterra
4 Replies

3. Shell Programming and Scripting

REGEX help required and some sed/awk help as well

Hi guys, I am coding a bash script that makes use of php scripts to pull URL's from a website. These url links will have numbers in them like 0.2.3 I want to make a regex that will yield me such numbers if I use a command like preg_grep. Question1: I need a regex that will tell my preg_grep... (2 Replies)
Discussion started by: mojoman
2 Replies

4. UNIX for Dummies Questions & Answers

Regex for one to four letters (sed) not GNUsed

I have regular sed on my computer. I am trying to find out a regex for one-four letters. I have tried (\{1,4\} This will match one or four characters, but what if the expression has two characters? Like AB1234 I don't have GNUsed and am having trouble with this regex. (5 Replies)
Discussion started by: newbie2010
5 Replies

5. Shell Programming and Scripting

Help with sed substitution / regex

Hi all, please can anyone show me how to use sed and regular expressions to achieve the following. If a line contains a capital A followed by exactly 5 or 6 characters followed by an angled bracket then insert an asterix before the angled bracket. So: XCONFIGA12345<X Becomes: ... (5 Replies)
Discussion started by: Jedimark
5 Replies

6. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

7. Shell Programming and Scripting

sed to awk (regex pattern) how?

Hello, I am trying to covert a for statement into a single awk script and I've got everything but one part. I also need to execute an external script when "not found", how can I do that ? for TXT in `find debugme -name "*.txt"` ;do FPATH=`echo $TXT | sed 's/\(.*\)\/\(.*\)/\1/'` how... (7 Replies)
Discussion started by: TehOne
7 Replies

8. Shell Programming and Scripting

sed - using regex and | need help

From my understanding when using regex1|regex2 the matching process tries each alternative in turn, from left to right, and the first one that succeeds is used. When im trying to extract the name from those examples: A) name.can.be.different.20.03.2009.boom B)... (2 Replies)
Discussion started by: TehOne
2 Replies

9. Shell Programming and Scripting

Sed and regex help needed

Hi all, I'm writing a script that replaces a value in a file. The file is formatted as follows: So, for this example, I'd like to replace the value for param_two. The value for param_two can be a one, or two-digit number. It replaces the value in file.cfg, and directs the... (9 Replies)
Discussion started by: marknu1
9 Replies

10. UNIX for Dummies Questions & Answers

sed regex

I would like to do this: replace the word "prod" with the word "special" but it may occur through the file naturally without a command, I only want it to happen when it has a specific command in front of it. The command will always look like this &lt;IMG,###,###,##,&gt;prod/directory/IMG/file ... (4 Replies)
Discussion started by: Shakey21
4 Replies
Login or Register to Ask a Question