LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 09-07-2011, 06:46 AM   #1
greenpool
Member
 
Registered: Sep 2010
Posts: 41

Rep: Reputation: 0
how to remove delimiter and append as one string


Hi,

got another question

i'm trying to create a wrapper script which enables the user to enter the date and time in the following manner:

date: 2011-06-31
time: 13:24

which needs to be converted into the following format:

date: 20110631
time: 132400 (00 need to add seconds ass 00)


can someone please give me some guidance in approaching this?

i'm purely doing this to educate myself. it doesn't really add any value but i'd like to know how to do it.

thanks!
 
Old 09-07-2011, 07:47 AM   #2
tbrand
Member
 
Registered: Jul 2006
Location: Toronto, Canada
Distribution: gentoo
Posts: 33

Rep: Reputation: 17
This may do it for you if you are using bash:

Code:
date=2011-06-31
time=13:24

date="${date:0:4}${date:5:2}${date:8:2}"
time="${time:0:2}${time:3:2}00"
You can read more about it when you look up string slicing in bash.

Last edited by tbrand; 09-07-2011 at 07:50 AM. Reason: Adding a suggestion on further reading
 
1 members found this post helpful.
Old 09-07-2011, 07:56 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
Bash is much easier than that:
Code:
date=2011-06-31
time=13:24

date=${date//-/}
time=${time/:/}00
 
1 members found this post helpful.
Old 09-07-2011, 08:33 AM   #4
salemeni
Member
 
Registered: Aug 2011
Posts: 64

Rep: Reputation: Disabled
Code:
date=`echo $date | sed -e "s/-//g"`
time=`echo $time"00" | sed -e "s/://g"`
java socket

Last edited by salemeni; 01-26-2012 at 08:27 AM.
 
1 members found this post helpful.
Old 09-07-2011, 08:49 AM   #5
Karl Godt
Member
 
Registered: Mar 2010
Location: Kiel , Germany
Distribution: once:SuSE6.2,Debian3.1, aurox9.2+3,Mandrake?,DSL? then:W7st,WVHB, #!8.10.02,PUPPY4.3.1 now:Macpup
Posts: 314

Rep: Reputation: 45
Another attempt using the cut command would be

Code:
DATE="$inputDATE" ## ex. inputDATE=2011-09-07
if [[ `echo "$DATE" | grep -o -e '\-[0-9]*\-'` = "" ]] ; then echo "Wrong formatted input for date" ; fi
YEAR=`echo "$inputDATE" | cut -f 1 -d "-"`
if [ "`echo $YEAR | wc -L`" -ne "4" ] ; then echo "Year too short or too long" ; fi
if [ -n "`echo $YEAR | grep -E '[[:punct:]]|[[:alpha:]]'`" ] ; then echo "Year incudes not only numbers" ; fi
MONT=`echo "$inputDATE" | cut -f 2 -d "-"`
## same checks for right input as for the year
if [ "$MONT" -gt "12" -o "$MONT" -lt "1" ] ; then echo "Month not in the range of 1-12" ; fi
DAAY=`echo "$inputDATE" | cut -f 3 -d "-"`
## same checks for right input as for the year and month
progFRORMATTEDdate="${YEAR}${MONT}${DAAY}"
## same now for the TIME ; HOUR=`echo "$inputTIME" | cut -f 1 -d ":"` ; MINU=`echo "$inputTIME" | cut -f 2 -d ":"`
Code checked with cygwin-bash on win7 .
http://lifehacker.com/179514/geek-to...-cygwin-part-i

Altrernatively simple would be filtering using grep
Code:
newDATE=`echo "$inputDATE" | grep -o '[0-9]' | tr -d '\n'`
 
Old 09-10-2011, 09:26 PM   #6
greenpool
Member
 
Registered: Sep 2010
Posts: 41

Original Poster
Rep: Reputation: 0
Thanks guys!
 
  


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
strtok behavior when character delimiter is the first char in string hajiman Programming 1 08-03-2011 08:14 PM
Append to string in awk Zmyrgel Programming 2 09-20-2009 08:50 AM
MySQL splitting a string with a delimiter and taking the 1st value and update Lantzvillian Programming 7 02-01-2008 12:57 AM
How to append a string to a file name? anandkj Linux - Newbie 2 01-10-2008 09:10 PM
Divide up lines with string delimiter elmu Programming 3 10-07-2005 09:48 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:26 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