sed搜索并用其子字符串值替换字符串

时间:2022-01-12 16:48:22

I have a Create Table Script ( External Table). I want to replace the table name as EXT_[First 27 characters of the original table]

我有一个创建表脚本(外部表)。我想将表名替换为EXT_ [原始表的前27个字符]

Sample I/P file

样本I / P文件

CREATE TABLE "P12345678912345678912345678912"
(
  "PARTITION_KEY" VARCHAR2(12),
  "ID" VARCHAR2(100),
  "STRESS_TESTING_SCENARIO_ID" VARCHAR2(100),
  "TRANCHE_COLLATERAL_TYPE" VARCHAR2(20),
  "TRANCHE_GUARANTEE_TYPE" VARCHAR2(20),
  "BS_TYPE" VARCHAR2(3),
  "CONTRACT_REFERENCE" VARCHAR2(50),

Expected O/p

CREATE TABLE "EXT_P12345678912345678912345678"
(
  "PARTITION_KEY" VARCHAR2(12),
  "ID" VARCHAR2(100),
  "STRESS_TESTING_SCENARIO_ID" VARCHAR2(100),
  "TRANCHE_COLLATERAL_TYPE" VARCHAR2(20),
  "TRANCHE_GUARANTEE_TYPE" VARCHAR2(20),
  "BS_TYPE" VARCHAR2(3),
  "CONTRACT_REFERENCE" VARCHAR2(50),

The script needs to work on both Linux and Solaris.

该脚本需要在Linux和Solaris上运行。

1 个解决方案

#1


this awk one-liner works for your example:

这个awk one-liner适用于你的例子:

awk -F'"' -v OFS='"' '/CREATE TABLE/{$2=substr($2,1,27);sub("^","EXT_",$2)}7' file

#1


this awk one-liner works for your example:

这个awk one-liner适用于你的例子:

awk -F'"' -v OFS='"' '/CREATE TABLE/{$2=substr($2,1,27);sub("^","EXT_",$2)}7' file