Match pattern and replace with string

hi guys,

insert into /*<new>*/abc_db.tbl_name

this is should be replaced to

insert into /*<new>*/${new}.tbl_name

it should use '.' as delimiter and replace

is there any way to do it using sed

sed 's/\(.*\/\)[^.]*\(\.tbl_name\)/\1${new}\2/' <<<"insert into /*<new>*/abc_db.tbl_name"
insert into /*<new>*/${new}.tbl_name

is that it?

tbl_name is nat same always

Can we do it like matching with /*<new>*/ then find until '.' and replace it with ${new} by not using abc_db

sed 's/\(.*\/\*<new>\*\/\)[^.]*\(\..*\)/\1${new}\2/' <<<"insert into /*<new>*/abc_db.tbl_name"
insert into /*<new>*/${new}.tbl_name

Thanks daPeach, It's working,
can you please explain wat it is doing?

---------- Post updated 09-04-09 at 01:53 PM ---------- Previous update was 09-03-09 at 10:05 PM ----------

how can we do this?

Input:

         	  
	  SELECT *
	  FROM
	    /*<new>*/abc_db./*<abc>*//*<hef>*/tbl_name
	  
	  WHERE
	        (   new_dt >= cast(/*<from_date>*/'2007-08-01' as date)
	        AND old_dt < cast(/*<to_date>*/'2007-08-02' as date)
	 	        )
	  ;

Output:

SELECT *
	  FROM
	    /*<new>*/${new}./*<abc>*//*<hef>*/${abc}${hef}_tbl_name
	  
	  WHERE
	        (   new_dt >= cast(/*<from_date>*/'${from_dt}' as date)
	        AND old_dt < cast(/*<to_date>*/'{to_date}' as date)
	 	        )
	  ;

How can we achevie this using sed, note that abc_db and tbl_name and dates change

cat sol_nov.infile
  SELECT *
  FROM
    /*<new>*/abc_db./*<abc>*//*<hef>*/tbl_name
  
  WHERE
        (   new_dt >= cast(/*<from_date>*/'2007-08-01' as date)
        AND old_dt < cast(/*<to_date>*/'2007-08-02' as date)
         )
  ;
sed "s@\(/\*<new>\*/\)[^.]*\(\./\*<[^>]*>\*//\*<[^>]*>\*/\)@\1${new}\2\${abc}\${hef}_@
s@\(.*/\*<from_date>\*/'\)[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\([^)]*\)@\1\${from_dt}\2@
s@\(.*/\*<to_date>\*/'\)[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\([^)]*\)@\1\${to_date}\2@" sol_nov-20090904.infile
  SELECT *
  FROM
    /*<new>*/./*<abc>*//*<hef>*/${abc}${hef}_tbl_name
  
  WHERE
        (   new_dt >= cast(/*<from_date>*/'${from_dt}' as date)
        AND old_dt < cast(/*<to_date>*/'${to_date}' as date)
         )
  ;

You really should take time to read some docs about sed.
It'll take longer for me to explain (and I'll be less clear), than for you to check the 'sed, a stream editor' page about these 'simple' regexps.

Or you can use the language with the mother lode of regexes:

$
$ cat f2
SELECT *
          FROM
            /*<new>*/abc_db./*<abc>*//*<hef>*/tbl_name
          WHERE
                (   new_dt >= cast(/*<from_date>*/'2007-08-01' as date)
                AND old_dt < cast(/*<to_date>*/'2007-08-02' as date)
                        )
          ;
$
$
$ ##
$ perl -nle 's!(\*/).*?(\.)!$1\${new}$2!;
>            s/(tbl_name)/\${abc}\${hef}_$1/;
>            /new_dt/ && s!\d{4}-\d\d-\d\d!\${from_dt}!;
>            /old_dt/ && s!\d{4}-\d\d-\d\d!\${to_date}!;
>            print' f2
SELECT *
          FROM
            /*<new>*/${new}./*<abc>*//*<hef>*/${abc}${hef}_tbl_name
          WHERE
                (   new_dt >= cast(/*<from_date>*/'${from_dt}' as date)
                AND old_dt < cast(/*<to_date>*/'${to_date}' as date)
                        )
          ;
$
$

tyler_durden