LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to remove delimiter and append as one string (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-remove-delimiter-and-append-as-one-string-901565/)

greenpool 09-07-2011 05:46 AM

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!

tbrand 09-07-2011 06:47 AM

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.

grail 09-07-2011 06:56 AM

Bash is much easier than that:
Code:

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

date=${date//-/}
time=${time/:/}00


salemeni 09-07-2011 07:33 AM

Code:

date=`echo $date | sed -e "s/-//g"`
time=`echo $time"00" | sed -e "s/://g"`

java socket

Karl Godt 09-07-2011 07:49 AM

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'`

greenpool 09-10-2011 08:26 PM

Thanks guys!


All times are GMT -5. The time now is 07:23 AM.