0

I am struggling to convert a textfile which contains the string:

10.04.2015 12:00:15

to

150410

The date will always be the date format but is taken from a textfile so as far as I know I can't use a date command and I think I must use awk but I don't know it well and am struggling - advice would be appreciated?

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
n34_panda
  • 2,577
  • 5
  • 24
  • 40
  • 3
    Note that the formats in your title are different to the ones in your input / desired output. – Tom Fenech Apr 16 '15 at 15:21
  • see http://stackoverflow.com/questions/6508819/convert-date-formats-in-bash – Fredrik Pihl Apr 16 '15 at 15:27
  • Cheers, I'll take the advice and use the one with more options for later use. – n34_panda Apr 16 '15 at 15:28
  • 1
    If it's the only data in the file then e.g. `awk '{ print substr($0, 7, 2) substr($0, 3, 2) substr($0, 1, 2) }'` but realistically, we need to understand what else there is in the file. Show us a real representative sample input and the desired output. – tripleee Apr 16 '15 at 15:33
  • 1
    If you could get rid of the dots and replace them with slashes, `date -d"10/04/2015 12:00:15" "+%y%d%m"` makes it. – fedorqui Apr 16 '15 at 15:40
  • `sed -e 's/\([0-9][0-9]*\)\.\([0-9][0-9]*\).[0-9][0-9]\([0-9][0-9]\)/\3\2\1/'` would do the work. No space to write a complete response filled. – Luis Colorado Apr 17 '15 at 11:05

3 Answers3

3

You can use bash parameter expansion to extract substrings

$ date="10.04.2015 12:00:15"
$ newdate=${date:8:2}${date:3:2}${date:0:2}
$ echo $newdate
150410
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • Looks like the best of all alternatives (if the shell is `bash`) – hek2mgl Apr 16 '15 at 16:06
  • 1
    ...and the input is a string, rather than a file (which I don't think is the case here) – Tom Fenech Apr 16 '15 at 16:10
  • @hek2mgl, and ksh93 and zsh – glenn jackman Apr 16 '15 at 17:19
  • Thanks, I did indeed go for this solution, the input is a string and I do use bash but ultimately I used this one as it is simply the easiest to see how it works. I am too new to sed/awk and regex's to fully understand everything but the parameter expansion is easily readable and easier to follow. Thanks, thanks for your help too Tom. – n34_panda Apr 17 '15 at 19:40
1

The following sed command should be good enough:

sed -r 's/([0-9]+)\.([0-9]+)\.[0-9]{2}([0-9]{2}) ([0-9]+:?){3}/\3\2\1/g' input

or awk:

awk -F'[.: ]' '{print substr($3,3),$2,$1}' OFS='' input

After all I prefer the latter.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • 1
    I ultimately went for the above siolution as it is simply easier to use for a beginner - note I tested these and they do the job nicely too. Thanks for the help/advice. – n34_panda Apr 17 '15 at 19:38
0

alternative answer, using awk:

DATE="10.04.2015 12:00:15"
echo $DATE | awk '{print substr($0,9,2) substr($0,4,2) substr($0,1,2)}'

Some explanation if you want to do modifications later:

  • $0 is the input text on which substr operates
  • the the first digit is where the substring starts
  • the second digit indicates the length of the substring
Slizzered
  • 869
  • 2
  • 9
  • 23
  • be carefull about *which contains the string* whre your code need a first extraction before occuring if it is inside a bigger string. Mybe assume that string to treat is only with this information – NeronLeVelu Apr 17 '15 at 09:01