简体   繁体   中英

Sed find and replace a character in a patten using regex

I have the following line and would like to replace the capital T with a blank space.

"2013-07-26T11:44:06.000+02:00"

I tried the following script but it did not work:

sed 's/^"/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]/T/""/g' file.csv

Since the date output won't generate any other T you can just go for :

sed 's/T/ /' file.csv

This will replace the first encountered T by a space.

What about using tr ?:

tr 'T' ' ' < test.csv

However, this is quite fragile as it would replace T also in other columns which doesn't contain dates. I gave this answer just as alternative for data which is not sensitive for that. For all other cases I would prefer @AvinashRaj's answer.

Try this,

sed -r 's/^\"([0-9]{4}-[0-9]{2}-[0-9]{2})(.)(.*)\"$/\"\1 \3\"/g' file

Example:

$ echo '"2013-07-26T11:44:06.000+02:00"' | sed -r 's/^\"([0-9]{4}-[0-9]{2}-[0-9]{2})(.)(.*)\"$/\"\1 \3\"/g'
"2013-07-26 11:44:06.000+02:00"

使用tr

tr 't' ' ' <<< "2013-07-26T11:44:06.000+02:00"

如果行以数字开头,则会将第一个“ T”更改为空白。

sed '/^[0-9]/s/T/ /'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM