感谢您的宝贵解释。但似乎我未能简要说明我的实际需求。所需的输入字符串和期望输出如下:
输入字符串:LINK:['IM219PLANNING_ARA1','IM219MIR_ARA1','IM18Q4_ARA1','SM18Q4_ARA1','IM118Q4DYNVA_ARA1','IM218Q4DYNVA_ARA1','IM119EIOPALTG_ARA1','IM219EIOPALTG_ARA1','SM119EIOPALTG_ARA1']}
所需输出:LINK:['IM219MIR_ARA1','IM18Q4_ARA1','SM18Q4_ARA1','IM118Q4DYNVA_ARA1','IM218Q4DYNVA_ARA1','IM119EIOPALTG_ARA1','IM219EIOPALTG_ARA1','SM119EIOPALTG_ARA1']}
此处的“IM219PLANNING_ARA1”是作为另一个 bash 脚本的一部分被删除的。此 bash 脚本有一个变量名“araname”,用户将在其中提供 LINK:[] 中指定的任何 ARA。所以我的工作是只删除该特定数组而不影响 javascript 语法。意思是删除开始和结束的单引号、逗号。此外,如果 araname 属于 LINK:[] 的结尾,则无需删除逗号。有运气吗?
答案1
另一种方法是保留最后一个“条目”:
sed -E 's/LINK:.*,(.+$)/[\1/' file
输出:
...
LINK:['SM119EIOPALTG_ARA1']}
答案2
这有效:
$ sed 's/LINK:\[.*\]/LINK:[]/g' sed_in > sed_out
$ cat sed_out
var externalARA= {
DESCRIPTION:['19Planning IM1 - Internal Model - GR/OE MANAGEMENT/REGULATORY - IM1V2119PLANNING_ARA1','19Planning IM2 - Internal Model - GR/OE MANAGEMENT/REGULATORY - IM219PLANNING_ARA1','19MIR IM2 - Internal Model - GR/OE MANAGEMENT/REGULATORY - IM219MIR_ARA1','18Q4 IM1 - Internal Model - GR/OE MANAGEMENT/REGULATORY - IM18Q4_ARA1','18Q4 SM1 - Standard Model - GR/OE REGULATORY - SM18Q4_ARA1','18DYNVA - IM1 - GR/OE MANAGEMENT/REGULATORY - IM118Q4DYNVA_ARA1','18DYNVA - IM2 - GR/OE MANAGEMENT/REGULATORY - IM218Q4DYNVA_ARA1','19EIOPALTG IM1 - Internal Model - GR/OE MANAGEMENT/REGULATORY - IM119EIOPALTG_ARA1','19EIOPALTG IM2 - Internal Model - GR/OE MANAGEMENT/REGULATORY - IM219EIOPALTG_ARA1','19EIOPALTG SM1 - Standard Model - GR/OE REGULATORY - SM119EIOPALTG_ARA','19Planning SM1 - Standard Model - GR/OE REGULATORY - SM119PLANNING_ARA'],
LINK:[]}
分解s/LINK:\[.*]/LINK:[]/g
:
s/LINK:\[.*\]
搜索以 开头LINK:[
和结尾的所有内容]
。因为[
是搜索控制字符,所以我们必须在其前面加上\
。/LINK:[]
用这个替换找到的所有内容。/g
虽然不是积极的,但我认为g
这是贪婪的,意味着不止一次地这样做。