Skip to main content
1 of 2
steeldriver
  • 83.9k
  • 12
  • 124
  • 175

If you use ed rather than sed you can use a regex address with negative offset:

/PRIMARY/-1 s/,$/)/ 

Ex.

$ printf '/PRIMARY/-1 s/,$/)/\n,p\nq\n' | ed -s file ID_SOUR_CALENDAR BIGINT NOT NULL DEFAULT 0 COMPRESS 0 ) UNIQUE PRIMARY INDEX ( CALENDAR_DATE ); ID , ID_SOUR , PRIMARY INDEX ( CALENDAR_DATE ); 

or (for in-place editing)

printf '/PRIMARY/-1 s/,$/)/\nwq\n' | ed -s file 

The replacement will only match the first instance of /PRIMARY/ unless preceded by the g modifier.

steeldriver
  • 83.9k
  • 12
  • 124
  • 175