0

I'd like to change this file eq.txt

2018-05-03T18:46:05.050Z 11.8314 44.0586 10 4.3 USGS
2018-05-01T05:16:59.270Z 11.0366 43.2334 10 4.1 USGS
2018-04-26T11:13:20.590Z -5.4109 36.5557 10 4 USGS
2018-04-25T09:48:42.940Z 14.8129 41.9053 10 4.3 USGS

to eq-new.txt

2018 05 03 18 46 05.050 11.8314 44.0586 10 4.3 USGS
2018 05 01 05 16 59.270 11.0366 43.2334 10 4.1 USGS
2018 04 26 11 13 20.590 -5.4109 36.5557 10 4 USGS
2018 04 25 09 48 42.940 14.8129 41.9053 10 4.3 USGS

I change the date and time format using

sed -r "s/([0-9.]+)-([0-9.]+)-([0-9.]+)T([0-9.]+):([0-9.]+):([0-9.]+)\.000Z/\1 \2 \3 \4 \5 \6.000/g" eq.txt

This works as I want.

However as I want to save the result in eq-new.txt, I do

sed -r "s/([0-9.]+)-([0-9.]+)-([0-9.]+)T([0-9.]+):([0-9.]+):([0-9.]+)\.000Z/\1 \2 \3 \4 \5 \6.000/g" eq.txt > eq-new.txt

Unfortunately, the output file is wrong, the date and time format in eq-new.txt is the same as in eq.txt

I've also tried this

tmp=$(sed -E "s/([0-9.]+)-([0-9.]+)-([0-9.]+)T([0-9.]+):([0-9.]+):([0-9.]+)\.000Z/\1 \2 \3 \4 \5 \6.000/g" tmp.txt)
echo -ne "$tmp" > eq-new.txt

No more success !

Nothing that I have found about sed and outfiles works.

Any ideas?

  • Are you sure the command is working when you don't redirect its output to a file? The answer you have is right, \.000Z matches a literal string containing three zeros; thus, in your sample data, the whole expression matches no line. Compare your command with sed -r "s/([0-9.]+)-([0-9.]+)-([0-9.]+)T([0-9.]+):([0-9.]+):([0-9.]+)Z/\1 \2 \3 \4 \5 \6/g". – fra-san Apr 19 at 10:45
1

Your pattern match times ending with .000 (literal) and no line contains such time. You basically have no matches, so replace is never triggered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.