irek
irek

Reputation: 21

sed remove multiple characters surrounded by digits

I have a file with following contents:

EMAIL|TESTNUMBER|DATE

[email protected]|123456789|2011-02-08T16:36:02Z

How do I remove capital letters T between the date and time and Z at the end of the line using sed?

Thanks!

Upvotes: 2

Views: 1501

Answers (3)

Rajish
Rajish

Reputation: 6795

Cat it through:

sed 's/\([0-9]+\)T\([0-9]+\)/\1\2//' | sed 's/Z$//'

Edit Oh my! I've just realized (thanks @Fredrik) that for a long time I wasted processes! Shame on me! Now I'm Church of The One Process convert. Here is the blessed version of the above abominated oneliner:

sed 's/\([0-9]+\)T\([0-9]+\)/\1\2//; s/Z$//'  the_file.txt

Upvotes: 0

BeerBrewerDan
BeerBrewerDan

Reputation: 1

Perhaps there's a fancier way, but the following script works for me:

s/\(....-..-..\)T\(.*\)/\1 \2/

s/Z$//

Example...in-bound file:

[email protected]|123456789|2011-02-08A16:36:02X
[email protected]|123456789|2011-02-08T16:36:02Z
[email protected]|123456789|2011-02-08B16:36:02Y

Output:

D:\>sed -f sedscr testfile

[email protected]|123456789|2011-02-08A16:36:02X
[email protected]|123456789|2011-02-08 16:36:02
[email protected]|123456789|2011-02-08B16:36:02Y

Upvotes: 0

William Pursell
William Pursell

Reputation: 212248

If the format is fixed and each line always matches T\d\d:\d\d:\d\dZ, then you could try the simple:

$ sed 's/T\(..:..:..\)Z$/ \1/'

(Untested)

Upvotes: 3

Related Questions