I have a text file named test.sql. How can I use the sed command to multiply the numbers in char and varchar by 2? Thank you.
The original content is:
create table test_table
(
test01 char(40),
test02 date,
test03 varchar(80),
test04 decimal(9,2) not null ,
test05 varchar(100),
test06 serial not null
);
I hope to use the sed command to change the content to the following:
create table test_table
(
test01 char(80),
test02 date,
test03 varchar(160),
test04 decimal(9,2) not null ,
test05 varchar(200),
test06 serial not null
);
I tried the following commands with no success.
sed -E 's:(char|varchar|nvarchar)\(([0-9]+)\):print \1\(\2 * 2\):g ' test.sql
sed -E "s/(char|varchar)\(([0-9]+)\)/\1\(echo "\\2*2"|bc\)/g" test.sql
[root@owen-test ~]# sed -E 's:(char|varchar|nvarchar)\(([0-9]+)\):print \1\(\2 * 2\):g ' test.sql
create table test_table
(
test01 print char(40 * 2),
test02 date,
test03 print varchar(80 * 2),
test04 decimal(9,2) not null ,
test05 print varchar(100 * 2),
test06 serial not null
);
[root@owen-test ~]# sed -E "s/(char|varchar)\(([0-9]+)\)/\1\(echo "\\2*2"|bc\)/g" test.sql
create table test_table
(
test01 char(echo 40*2|bc),
test02 date,
test03 varchar(echo 80*2|bc),
test04 decimal(9,2) not null ,
test05 varchar(echo 100*2|bc),
test06 serial not null
);
It is impossible to multiply a number using sed. Use a different tool.
sed
is a string replacement tool. It can replace 80
by 160
and replace 40
by 80
. You can then hardcode each possible number and that way implement arithmetic using string replacements. It would result it in a very big boring long hobby project.
Use a different tool. Typically, when sed
is not enough or becomes unreadable, use awk
. Or use python or perl.
When using GNU sed
, then sed
can call the shell, and shell can then calculate the value or call another external program to do the calculation. Still this looks way way way easier with GNU awk:
awk '
match($0, /(.*)(char|varchar|varchar)\(([0-9]+)\)(.*)/, arr) {
$0 = arr[1] arr[2] "(" (arr[3] * 2) ")" arr[4];
}
1
'