You're viewing a comment by JESS and its responses.

JESS Permalink
February 09, 2013, 00:06

Hi, Can you please help me with the following question.

I'm using sed to add HELLO at the end of the each DATE* word.

$ echo $TAB_COLS
ID,TITLE,DATE_PROG1,DATE_PROG2

I get the following.
$ echo $TAB_COLS | sed -e 's/\(,DATE.*\),*/\1 \"HELLO\"/g'
ID,TITLE,DATE_PROG1,DATE_PROG2 "HELLO"

But, I expact the output as copied below.

ID,TITLE,DATE_PROG1 "HELLO",DATE_PROG2 "HELLO"

How can I achive this? Does this have to do anything with sed grediness?

Thanks for your help.

Comment Responses

February 14, 2013, 00:39

Yes, that's because .* is greedy.

This \(,DATE.*\),* matches ,DATE_PROG1,DATE_PROG2, and replaces it with ,DATE_PROG1,DATE_PROG2 "HELLO".

There is no way to make .* non-greedy in sed. You've to use [...]* trick:

$ echo $TAB_COLS | sed -e 's/\(DATE[^,]*\)/\1 \"HELLO\"/g'
ID,TITLE,DATE_PROG1 "HELLO",DATE_PROG2 "HELLO"

Reply To This Comment

(why do I need your e-mail?)

(Your twitter handle, if you have one.)

Type the word "security_95": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.