navya chamidisetty
navya chamidisetty

Reputation: 371

How to replace a string between two commas in linux using sed

execute PKG_SP_MAINTENANCE.MoveAccount(91, 129031, 958408630);          Lowes

From the above statement I am trying to get the content between the first comma and second comma i.e., 129031 and replace it with a new string which is passed as a parameter to the script. For now let's replace with N . I tried the following sed command ended up getting an error. Could someone please help?

04:24:01 Tue Sep 19 [serviceb@LQASRDSBMST002V:~/isops/tmp] cat Navya | sed 's/,^.\{*\},/N/'
sed: -e expression #1, char 14: Invalid content of \{\}

Upvotes: 0

Views: 2346

Answers (1)

EdmCoff
EdmCoff

Reputation: 3576

$: echo "start,middle,end" | sed 's/,[^,]*,/,NEW,/g' 
start,NEW,end

Is this what you mean? This simply matches the inner-most commas and replaces the text.

Depending how you want to handle strings with more than two commas, you could do something like this to match the outer-most instead:

$: echo "start,middle,end" | sed 's/,.*,/,NEW,/g'
start,NEW,end

Upvotes: 1

Related Questions