shantanuo
shantanuo

Reputation: 32326

Remove all words after starting from a specific word

How do I remove all the words starting from COMMENT word in the following string?

echo "first_mail_sent_date_time datetime NOT NULL  COMMENT 'The date and time',"  |  sed 's/ COMMENT \w*/ /g'

The above sed command will remove only the word COMMENT. I need to remove everything after COMMENT but keep the last comma.

I can use awk but I am looking for sed solution for consistency sake.

awk -F'COMMENT' '{print $1 ","}'

Upvotes: 1

Views: 11630

Answers (2)

potong
potong

Reputation: 58430

This might work for you (GNU sed):

sed 's/ COMMENT.*,/,/' file

Upvotes: 2

anubhava
anubhava

Reputation: 785196

Use this sed command:

sed "s/ COMMENT.*'[^']*'/ /"

TEST:

str="first_mail_sent_date_time datetime NOT NULL COMMENT 'The date and time'FOO,"
echo $str | sed "s/ COMMENT.*'[^']*'/ /"

OUTPUT:

first_mail_sent_date_time datetime NOT NULL FOO,

Upvotes: 1

Related Questions