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?

6
  • 3
    Note that the formats in your title are different to the ones in your input / desired output. Commented Apr 16, 2015 at 15:21
  • see stackoverflow.com/questions/6508819/… Commented Apr 16, 2015 at 15:27
  • Cheers, I'll take the advice and use the one with more options for later use. Commented Apr 16, 2015 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. Commented Apr 16, 2015 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. Commented Apr 16, 2015 at 15:40

3 Answers 3

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 
Sign up to request clarification or add additional context in comments.

4 Comments

Looks like the best of all alternatives (if the shell is bash)
...and the input is a string, rather than a file (which I don't think is the case here)
@hek2mgl, and ksh93 and zsh
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.
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.

1 Comment

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.
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

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.