Skip to main content
corrrected local / global
Source Link
steeldriver
  • 83.9k
  • 12
  • 124
  • 175

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

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

Ex.

$ printf ''g/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 ''g/PRIMARY/-1 s/,$/)/\nwq\n' | ed -s file 

The replacement will only match the first instanceALL instances of /PRIMARY/ unless preceded by- if you only want to replace the first, remove the g modifier.

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.

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

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

Ex.

$ printf 'g/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 'g/PRIMARY/-1 s/,$/)/\nwq\n' | ed -s file 

The replacement will match ALL instances of /PRIMARY/ - if you only want to replace the first, remove the g modifier.

Source Link
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.