Reputation: 212452
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
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 #1Upvotes: 2
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