JBoy
JBoy

Reputation: 5755

small issue with regex in sed

i have this string:

 data="week 50; data; data; data; data"

i would like to erase the week part from it by using sed, so that i would get: data; data; data; data I have come up with this but apparently is not working, any idea why? Where is the wrong rule?

code:

echo ${data} | sed '/%week.*%;/s/%week.*%;//'

Chers

Upvotes: 2

Views: 76

Answers (2)

Ed Morton
Ed Morton

Reputation: 204638

Do you really want to remove the semi-colon-separated field containing text that starts with "week" or is it really that you just want to remove the first field? Consider:

$ echo "week 50; data; data; data; data" | cut -d\; -f2-
 data; data; data; data

$ echo "week 50; data; data; data; data" | sed 's/[^;]*;//'
 data; data; data; data

Upvotes: 1

Thor
Thor

Reputation: 47229

This should be enough:

echo ${data} | sed 's/week[^;]*;//'

Output:

data; data; data; data

This matches week plus up-to and including the next semi-colon ([^;]*;) and removes it.

If you also want to remove the extraneous space, do this:

 echo ${data} | sed 's/week[^;]*; *//'

Upvotes: 5

Related Questions