# Match pattern and replace with string

**URL:** <https://community.unix.com/t/match-pattern-and-replace-with-string/248594>\
**Category:** Shell Programming and Scripting\
**Created:** [September 3, 2009, 7:50pm UTC](https://community.unix.com/t/match-pattern-and-replace-with-string/248594 "2009-09-03T19:50:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sol\_nov](https://community.unix.com/letter_avatar/sol_nov/32/6_22734026db12803ebb3e059622ac3534.png) [@sol\_nov](https://community.unix.com/u/sol_nov)\
**Post date:** [September 3, 2009, 7:50pm UTC](https://community.unix.com/t/match-pattern-and-replace-with-string/248594/1 "2009-09-03T19:50:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![daPeach](https://community.unix.com/letter_avatar/dapeach/32/6_22734026db12803ebb3e059622ac3534.png) [@daPeach](https://community.unix.com/u/daPeach)\
**Post date:** [September 3, 2009, 8:06pm UTC](https://community.unix.com/t/match-pattern-and-replace-with-string/248594/2 "2009-09-03T20:06:50Z")

</div>

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

```

is that it?

---

<div class="post-metadata">

**Author:** ![sol\_nov](https://community.unix.com/letter_avatar/sol_nov/32/6_22734026db12803ebb3e059622ac3534.png) [@sol\_nov](https://community.unix.com/u/sol_nov)\
**Post date:** [September 3, 2009, 8:15pm UTC](https://community.unix.com/t/match-pattern-and-replace-with-string/248594/3 "2009-09-03T20:15:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![daPeach](https://community.unix.com/letter_avatar/dapeach/32/6_22734026db12803ebb3e059622ac3534.png) [@daPeach](https://community.unix.com/u/daPeach)\
**Post date:** [September 3, 2009, 9:39pm UTC](https://community.unix.com/t/match-pattern-and-replace-with-string/248594/4 "2009-09-03T21:39:52Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![sol\_nov](https://community.unix.com/letter_avatar/sol_nov/32/6_22734026db12803ebb3e059622ac3534.png) [@sol\_nov](https://community.unix.com/u/sol_nov)\
**Post date:** [September 4, 2009, 2:53pm UTC](https://community.unix.com/t/match-pattern-and-replace-with-string/248594/5 "2009-09-04T14:53:17Z")

</div>

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:

```nohighlight
         	  
	  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:

```nohighlight
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

---

<div class="post-metadata">

**Author:** ![daPeach](https://community.unix.com/letter_avatar/dapeach/32/6_22734026db12803ebb3e059622ac3534.png) [@daPeach](https://community.unix.com/u/daPeach)\
**Post date:** [September 4, 2009, 6:03pm UTC](https://community.unix.com/t/match-pattern-and-replace-with-string/248594/6 "2009-09-04T18:03:01Z")

</div>

```plaintext
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](http://www.google.com/search?q=gnu+sed+a+stream+editor&ie=utf-8&oe=utf-8&aq=t&rls=org.debian:fr:unofficial&client=iceweasel-a) 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.

---

<div class="post-metadata">

**Author:** ![durden\_tyler](https://community.unix.com/user_avatar/community.unix.com/durden_tyler/32/1450_2.png) [@durden\_tyler](https://community.unix.com/u/durden_tyler)\
**Post date:** [September 4, 2009, 7:56pm UTC](https://community.unix.com/t/match-pattern-and-replace-with-string/248594/7 "2009-09-04T19:56:52Z")

</div>

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

```nohighlight
$
$ 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
