LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-15-2012, 08:13 AM   #1
pixiandreas
LQ Newbie
 
Registered: May 2012
Posts: 7

Rep: Reputation: Disabled
Regex with sed


Hello i have a file that contains date and time format,
what i want to do is to replace the seconds field with zeros(00)

Example:

Before:
Code:
2011-01-27 03:50:13	156109680	226945640	
2011-01-27 03:51:02	156109960	226945960	
2011-01-27 03:52:01	156110250	226946280	
2011-01-27 03:53:45	156110540	226946600
After:
Code:
2011-01-27 03:50:00	156109680	226945640	
2011-01-27 03:51:00	156109960	226945960	
2011-01-27 03:52:00	156110250	226946280	
2011-01-27 03:53:00	156110540	226946600
I start with a sed command but i replace, minutes and seconds to 00

sed -e 's/:[0-5][0-9]^:/:00/g' file.txt

I have read several pages but i doesn't understand how to do this


Can anybody help me with this problem ??

Last edited by colucix; 05-15-2012 at 01:18 PM. Reason: Added CODE tags
 
Old 05-15-2012, 08:18 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
the command works as:
s/<search regexp>/<replacement>/
so you need to search for :digitdigitspace and replace with :00 (including space at the end)
So instead of ^: at the end of your search expression:
s/:[0-5][0-9] /:00 / would be better (g is not required)
 
Old 05-15-2012, 08:18 AM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Try:

Code:
sed 's/:[0-5][0-9] /:00 /' file.txt
Please note the spaces after 9] and 00
 
Old 05-15-2012, 08:38 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Even simpler:
Code:
sed 's/:.. /:00 /1' file
 
Old 05-15-2012, 08:44 AM   #5
pixiandreas
LQ Newbie
 
Registered: May 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Hello Thanks for really fast answer :-)

When i try this commands i get the same results :-(




andreas@server:~$ sed 's/:[0-5][0-9] /:00 /' file.txt
2011-01-27 03:50:13 156109680 226945640
2011-01-27 03:51:02 156109960 226945960
2011-01-27 03:52:01 156110250 226946280
2011-01-27 03:53:45 156110540 226946600

andreas@server:~$ sed 's/:.. /:00 /1' file.txt
2011-01-27 03:50:13 156109680 226945640
2011-01-27 03:51:02 156109960 226945960
2011-01-27 03:52:01 156110250 226946280
2011-01-27 03:53:45 156110540 226946600


/Andreas H
 
Old 05-15-2012, 08:54 AM   #6
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Can you post a sample of the file wrapped in the code tags?
Are those spaces or tabs?

What version of sed are you using?
 
Old 05-15-2012, 09:00 AM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
you can use ^: - between [ and ] instead of space:
sed 's/:..[^:]/:00 /'
maybe it works
 
Old 05-15-2012, 09:31 AM   #8
pixiandreas
LQ Newbie
 
Registered: May 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thanks everyone this solution work perfect.

Code:
sed 's/:..[^:]/:00 /'
Code:
Next time i will ask for something i will put the code inside code tags ;-)
/Andreas H
 
Old 05-15-2012, 09:47 AM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post.
If you want to highlight your sentences just use bold or italic text, but not code.
I'm glad to help you.
 
Old 05-15-2012, 10:09 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
I think I would use awk instead. It just seems cleaner, as you can target the time field only:

Code:
awk '{ sub(/..$/,"00",$2); print }' file

And here are some references for you.

sed:
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt

awk:
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/blog/awk-one...ined-part-one/
 
Old 05-15-2012, 12:55 PM   #11
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
Quote:
Originally Posted by David the H. View Post
I think I would use awk instead. It just seems cleaner, as you can target the time field only:

Code:
awk '{ sub(/..$/,"00",$2); print }' file
nice, and if you want to write less:
Code:
awk ' sub(/..$/, "00", $2) ' file
is enough
 
Old 05-15-2012, 01:22 PM   #12
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by sycamorex View Post
Can you post a sample of the file wrapped in the code tags?
Are those spaces or tabs?

What version of sed are you using?
sycamorex, you hit the nail in the head. I've added CODE tags and they reveal the presence of TABs. Andreas H and pan64 you're kindly invited to use CODE tags in the future, for both the questions and the answers. Thank you.
 
Old 05-16-2012, 03:30 PM   #13
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by pan64 View Post
nice, and if you want to write less:
Code:
awk ' sub(/..$/, "00", $2) ' file
is enough
Of course. But my general policy is to give the traditional explicit syntax for new scripters. Posting advanced shortcuts, at least without including detailed explanations of them, tends to confuse more than help. They also tend to be rather situation-specific and not easily adaptable to more generalized use.

More experienced users can usually figure out how to shorten it on their own.


For the record, the above code works because of the basic awk 'pattern { commands }' syntax. If you leave the bracketed commands off and only have a pattern, then it defaults to printing the line on a true evaluation. In this case, the sub function returns true when a valid substitution is made, and so the line prints.

Note though that because this relies on the status of sub to perform the action, if there happened to be any lines in the file that didn't contain a "$2" field, they wouldn't get printed. But the explicit declaration of the action in my version prints every line regardless of the substitution result.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed and regex help zski128 Programming 5 12-13-2011 11:30 AM
sed regex say_hi_ravi Programming 3 09-15-2011 03:12 AM
[SOLVED] sed regex schneidz Programming 1 02-28-2011 07:46 PM
regex with sed to process file, need help on regex dwynter Linux - Newbie 5 08-31-2007 06:10 AM
Help with Sed and regex cmfarley19 Programming 6 11-18-2004 02:09 PM


All times are GMT -5. The time now is 04:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration