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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
05-15-2012, 07:13 AM
|
#1
|
LQ Newbie
Registered: May 2012
Posts: 7
Rep: 
|
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 12:18 PM.
Reason: Added CODE tags
|
|
|
05-15-2012, 07:18 AM
|
#2
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,676
|
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)
|
|
|
05-15-2012, 07:18 AM
|
#3
|
LQ Veteran
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
|
Try:
Code:
sed 's/:[0-5][0-9] /:00 /' file.txt
Please note the spaces after 9] and 00
|
|
|
05-15-2012, 07:38 AM
|
#4
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Even simpler:
Code:
sed 's/:.. /:00 /1' file
|
|
|
05-15-2012, 07:44 AM
|
#5
|
LQ Newbie
Registered: May 2012
Posts: 7
Original Poster
Rep: 
|
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
|
|
|
05-15-2012, 07:54 AM
|
#6
|
LQ Veteran
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
|
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?
|
|
|
05-15-2012, 08:00 AM
|
#7
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,676
|
you can use ^: - between [ and ] instead of space:
sed 's/:..[^:]/:00 /'
maybe it works
|
|
|
05-15-2012, 08:31 AM
|
#8
|
LQ Newbie
Registered: May 2012
Posts: 7
Original Poster
Rep: 
|
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
|
|
|
05-15-2012, 08:47 AM
|
#9
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,676
|
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.
|
|
|
05-15-2012, 09:09 AM
|
#10
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
|
|
|
05-15-2012, 11:55 AM
|
#11
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,676
|
Quote:
Originally Posted by David the H.
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
|
|
|
05-15-2012, 12:22 PM
|
#12
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Quote:
Originally Posted by sycamorex
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.
|
|
|
05-16-2012, 02:30 PM
|
#13
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
Quote:
Originally Posted by pan64
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.
|
|
|
All times are GMT -5. The time now is 05:53 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|