Bash / AWK / SED Match and rewrite a string of numbers (date) in a string

I have a text file with the following contents repeating about 60 times from a converted .ics file:

Start Vak
Tijd van: 20120411T093000Z
Tijd tot: 20120411T100000Z
Klas(sen) en Docent(en): VPOS0A1 VPOS0A2 Mariel Kers
Vak: Ex. Verst. beperk.
Lokaal: 7.05
Einde Vak

I want to rewrite the values โ€‹โ€‹of "Tijd van" and "Tijd tot" so that they become a good date (in a bash script on a GNU / Linux system with awk, sed and grep, etc.). I tried using awk to find it:

awk '/^Tijd.*[:digit:][:digit:]Z$/; { getline; print $0; }' rooster2.txt

and grep:

egrep '/^Tijd(.*)[:digit:][:digit:]Z$/' rooster2.txt

But they both do not even find the line.

I want to rewrite this date in a more acceptable time format, for example, EPOCH or something like 04/31/2012 13:00:00. I do not want to replace or rewrite the entire line, only a specific line! Any advice, examples or links are welcome and very helpful.

+4
source share
3 answers

Try this (GNU sed):

sed -r 's/(Tijd ...: )(....)(..)(..).(..)(..)(..)./\1 \4.\3.\2 \5:\6:\7/' FILE
+3

; time Time.parse, regexp. strftime .

[slmn@uriel ~]$ ruby -rtime -ne 'puts $_.sub(/(Tijd (van|tot): )(.*)/) { $1 + Time.parse($3).strftime("%D %T") }' < yourfile.txt
Start Vak
Tijd van: 04/11/12 09:30:00
Tijd tot: 04/11/12 10:00:00
Klas(sen) en Docent(en): VPOS0A1 VPOS0A2 Mariel Kers
Vak: Ex. Verst. beperk.
Lokaal: 7.05
Einde Vak
+1

awk :

  • [:digit:] " ", ([...]): [[:digit:]] ( , "a, _", [a[:digit:]_], , .)
  • (;) (/.../) ({...}) , , {print $0} , (.. ).
  • getline awk (, ), .

, :

  • , /^Tijd.*[:digit:][:digit:]Z$/ ( , [:digit:] " :, d, i, g t" ).
  • , : .

, , ( , ).

, , " " Tijd " , " Z ", :

awk '/^Tijd.*[[:digit:]][[:digit:]]Z$/{ print $0; }' rooster2.txt

{print $0} ,

awk '/^Tijd.*[[:digit:]][[:digit:]]Z$/' rooster2.txt

, - :

awk '/^Tijd.*[[:digit:]][[:digit:]]Z$/{year=substr($NF,1,4);month=substr($NF,5,2);day=substr($NF,7,2);hour=substr($NF,10,2);min=substr($NF,12,2);sec=substr($NF,14,2);$NF=day"."month"."year" "hour":"min":"sec}1' rooster2.txt

:

  • (, ), (/.../), ($NF) .
  • (.. ) (1 - , (.. ) , ({print $0}))

, GNU awk strftime. , . , , :

awk -v FORMAT="%c" '/^Tijd.*[[:digit:]][[:digit:]]Z$/{$NF=strftime(FORMAT,mktime(substr($NF,1,4)" "substr($NF,5,2)" "substr($NF,7,2)" "substr($NF,10,2)" "substr($NF,12,2)" "substr($NF,14,2)))}1' rooster2.txt

FORMAT . . man strftime.

+1

All Articles