Visit Our UNIX and Linux User Community

Sed regex problem

Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Sed regex problem
# 1  
Old 11-11-2010
Sed regex problem


I tried to extract the time from `date` with sed.
(I know it works with `date +%H:%M:%S` as well)

I got three solutions of which just one worked. I thought "+" should repeat the previous expression 1 or more times and {n} should repeat the previous expression n times.
$ date
Thu Nov 11 17:19:03 CET 2010
$ date | sed 's/^.*\([0-9]+:[0-9]+:[0-9]+\).*$/\1/'
Thu Nov 11 17:19:05 CET 2010
$ date | sed 's/^.*\([0-9]{1}:[0-9]{1}:[0-9]{1}\).*$/\1/'
Thu Nov 11 17:19:11 CET 2010
$ date | sed 's/^.*\([0-9][0-9]:[0-9][0-9]:[0-9][0-9]\).*$/\1/' 

I just want to know what's wrong with the examples.
  • Linux 2.6.31-22-generic #68-Ubuntu SMP Tue Oct 26 16:38:35 UTC 2010 i686 GNU/Linux
  • bash shell
# 2  
Old 11-11-2010
[ctsgnb@shell ~]$ date | sed 's/^.*\([0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}\).*$/\1/'

# 3  
Old 11-11-2010

Why do I have to escape something like * and . to get the character and escape + and { to get the regex?

Isn't that a kind of counter logic?

Thanks for the help
# 4  
Old 11-11-2010
you could more simply use
date | sed 's/^.*\(..:..:..\).*$/\1/'

# 5  
Old 11-11-2010
sure, I could also use
date | awk '{print $4}'

# 6  
Old 11-11-2010
# date | cut -d" " -f4

# 7  
Old 11-11-2010
but the best would still be date +%H:%M:%S

---------- Post updated at 07:57 PM ---------- Previous update was at 07:17 PM ----------

Originally Posted by thiuda
Why do I have to escape something like * and . to get the character and escape + and { to get the regex?

Isn't that a kind of counter logic?

Thanks for the help
The + sign is part of extended set of metacharacters used by egrep and awk and maybe supported by some new sed version but is not part of old standard implementation of sed.

could just be written such as

but i encountered the following case where the * match the shortest instead of the longest (on FreeBSD)
(i don't know whether this behaviour could be tweaked somewhere...) :

[ctsgnb@shell ~]$ date | sed 's/^.*\([0-9][0-9]*:[0-9][0-9]*:[0-9][0-9]*\).*$/\1/'
[ctsgnb@shell ~]$ uname -a
FreeBSD <anonymized> 8.1-RELEASE FreeBSD 8.1-RELEASE #0: Sun Jul 25 16:41:25 MDT 2010     <anonymized>:/usr/obj/usr/src/sys/CJB  amd64
[ctsgnb@shell ~]$

ooops, i just got aware that ...
... in fact the first * match the longest so that make the second one match the shortest this could lead to tricky unexpected result
so... the space matter :
[ctsgnb@shell ~]$ date | sed 's/^.*\([0-9][0-9]*:[0-9][0-9]*:[0-9][0-9]*\).*$/\1/'
[ctsgnb@shell ~]$ date | sed 's/^.* \([0-9][0-9]*:[0-9][0-9]*:[0-9][0-9]*\).*$/\1/'
[ctsgnb@shell ~]$

Last edited by ctsgnb; 11-11-2010 at 04:28 PM..
This User Gave Thanks to ctsgnb For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #894
Difficulty: Medium
A tr command is also part of ASCII's MSX-DOS2 Tools for MSX-DOS version 2.
True or False?

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) 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

Featured Tech Videos