简体   繁体   中英

Sed remove time stamp from file with regex

I'm trying to use sed to delete a pattern from an html file. The time stamp consist of a 1-2 digit number a four letter word and then the word ago

example:

25 mins ago

or:

1 hour ago

and so on. I've tried using sed like this:

sed -i "s/([0-9]{1,2}) [a-z]* ago//g"

Sed does nothing, i'm not sure if my regex is wrong or if I am not escaping characters the right way.

Edit: I fixed that expression by removing an extra space, thanks choroba. Now sed removes mos of the text from the file. The expression needs to be less greedy? should also mention that ever time stamp is surrounded by > < example:

>1 hour ago<

Edit: This is what worked for me. Thanks ravoori.

sed -i 's/[0-9]\{,2\} [[:alpha:]]\{4,5\} ago//g'

Any help is appreciated!

Try the below. You need to escape the quantifier metacharacters { and } with sed

echo "1 hour ago" | sed  's/[0-9]\{,2\} [[:alpha:]]\{4\} ago//g'
 echo "1 hour ago" | sed -e 's/.*ago$//g'

or

 sed -e 's/.*ago$//g' <filename>

This should remove any line that ends with ago in filename

You may not want to use this if you have other lines than just the timestamp that ends with ago . You didn't specify.

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