Mark Baker
Mark Baker

Reputation: 212452

sed search/replace with wildcard

I'm trying to use sed to do a search/replace in a set of text files. Where the files contain lines like:

$table->char('widget_guid', 36)->index('widget_guid');

I'm looking to replace the char with guid and strip out the , 36, so the resulting line would look like

$table->guid('widget_guid')->index('widget_guid');

My effort,

sed -i 's/char(\('.*'), 36\)/guid\(\1\)/g' *create*.php

but nothing is being replaced

I've tested the expression using regexp101.com and that shows it should picking up the correct blocks, with the correct capture group

Any suggestions as to what I might be doing wrong? And how to achieve what I want?

Upvotes: 1

Views: 6450

Answers (2)

anubhava
anubhava

Reputation: 785531

You can use this sed:

sed -E 's/char(\([^,]*)[^)]*/guid\1/' file

$table->guid('widget_guid')->index('widget_guid');

Explanation:

  • char: Match string char
  • (: Start capture group #1
    • \(: Match literal (
    • [^,]*: Match 0 or more of any characters that are not comma
  • ): End capture group #1
  • [^)]*: Match 0 or more of any characters that are not )
  • -E enables extended regex (ERE) instead of basix regex (BRE) in sed
  • \1 is back-reference of capture group #1

Upvotes: 2

RavinderSingh13
RavinderSingh13

Reputation: 133610

Following sed may help you in same.

sed  's/char/guid/;s/, [0-9]*//'  Input_file

Output will be as follows.

$table->guid('widget_guid')->index('widget_guid');

Upvotes: 0

Related Questions