LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-14-2012, 06:12 PM   #1
GamezR2EZ
Member
 
Registered: Jan 2009
Posts: 31

Rep: Reputation: 4
Replace last delimiter BASH


I have a timecode that I need to change. It is in the format of 00:00:00 It needs to change to 00:00.00

The code I have so far:

Code:
timecode=00:00:00
newtimecode=$(echo $timecode | cut -d: -f1):$(echo $timecode | cut -d: -f2).$(echo $timecode | cut -d: -f3))
Is there a cleaner way to do this? Something like
Code:
newtimecode=$(echo $timecode | sed -e 's/:$//')
Obviously that doesn't work unless the last character is :
Does anyone have a cleaner solution, or should I work with what I have?
 
Old 02-14-2012, 06:25 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
You can try the following:
Code:
sed 's/:/./2' file
It's going to replace the second occurrence of : with .
 
1 members found this post helpful.
Old 02-14-2012, 07:45 PM   #3
GamezR2EZ
Member
 
Registered: Jan 2009
Posts: 31

Original Poster
Rep: Reputation: 4
That is perfect, thank you!

Not for this problem as I know the variable format, but for future reference is there a way to specify the last occurrence of a character in sed?
 
Old 02-14-2012, 10:34 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
How about you just let bash do its thing:
Code:
newtimecode=${timecode%:*}.${timecode##*:}
 
Old 02-15-2012, 01:18 PM   #5
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
Originally Posted by GamezR2EZ View Post
but for future reference is there a way to specify the last occurrence of a character in sed?
I can't think of any generic way to be applicable in every possible scenario. There's always a way if we discuss a specific example.
 
  


Reply



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
How use CUT -d 'delimiter' is delimiter is a TAB? frenchn00b Programming 12 11-06-2013 03:17 AM
BASH -copy stdin to stdout (replace cat) (bash browser) gnashley Programming 4 07-21-2008 01:14 PM
Search and Replace from bash hammertime1983 Linux - Newbie 4 11-02-2007 03:18 PM
Bash - txt replace clpl1980 Fedora 2 01-22-2007 05:44 AM
howto replace bash ? pingvina Linux - Software 12 12-30-2005 02:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:32 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
Open Source Consulting | Domain Registration