0

How do i change the following input line using SED ?

Input - bus_ln in ('abc');

Required O/P - bus_ln in ('def','xyz');

4 Answers 4

1

Give this a try:

sed "s/\([^(]*\)('[^']*')/\1('def','xyz')/" inputfile

It will replace whatever is between the parentheses.

Input:

bus_ln in ('abc');
foo('bar');
baz aaa bbb ('ccc ddd') some more text

Output:

bus_ln in ('def','xyz');
foo('def','xyz');
baz aaa bbb ('def','xyz') some more text
Sign up to request clarification or add additional context in comments.

Comments

0

Using sed substitute command:

echo "bus_ln in ('abc');" | sed "s/bus_ln in ('abc');/bus_ln in ('def','xyz');/"

Comments

0
$ echo "Input - bus_ln in ('abc');" | ruby -e 'print gets.gsub(/(.*\()(.[^;]*)(\);)/,"\\1\047def\047,\047xyz\047\\3")'
Input - bus_ln in ('def','xyz');

Comments

0

This might work for you:

echo "bus_ln in ('abc');" | sed "s/'.*'/'def','xyz'/"
bus_ln in ('def','xyz');

This uses "'s to surround the sed commands which may sometimes lead to unexpected results. Using ''s instead is clumsy (in comparison) but more predictable:

echo "bus_ln in ('abc');" | sed 's/'\''.*'\''/'\''def'\',\''xyz'\''/'
bus_ln in ('def','xyz');

Where '\'' represents a single ' and '\',\'' represents ','

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.