1

Thanks for your valuable explanations. But it seems that I've failed to brief my actual requirement. The input string and desired output required were given below:

input string: LINK:['IM219PLANNING_ARA1','IM219MIR_ARA1','IM18Q4_ARA1','SM18Q4_ARA1','IM118Q4DYNVA_ARA1','IM218Q4DYNVA_ARA1','IM119EIOPALTG_ARA1','IM219EIOPALTG_ARA1','SM119EIOPALTG_ARA1']}

Desired output: LINK:['IM219MIR_ARA1','IM18Q4_ARA1','SM18Q4_ARA1','IM118Q4DYNVA_ARA1','IM218Q4DYNVA_ARA1','IM119EIOPALTG_ARA1','IM219EIOPALTG_ARA1','SM119EIOPALTG_ARA1']}

Here 'IM219PLANNING_ARA1', was removed as part of an another bash script. This bash script is having a variable name "araname" in which user will provide any of the ARA's specified inside the LINK:[]. So my job is remove that particular array only without affecting the javascript syntax. Mean remove start and end single quotes, comma. Also if araname is belongs to end of LINK:[] then no need to remove comma. Any luck?

  • So the output is just LINK:[]? – guillermo chamorro Dec 4 '19 at 11:21
  • 1
    Please edit your question and show us your desired output. It is very hard to understand from the description. Do you only need to remove IM219PLANNING_ARA1 or do you need to remove everything inside the LINK[ ]? – terdon Dec 4 '19 at 11:22
  • this is my though I think OP want to know how to remove one word, his file is like a DB, so at first he want to know how to remove one word (like IM219PLANNING_ARA1), and he would apply this same command for the DESCRIPTION line. So result for LINK line would be : LINK:['IM219MIR_ARA1','IM18Q4_ARA1','SM18Q4_ARA1','IM118Q4DYNVA_ARA1','IM218Q4DYNVA_ARA1','IM119EIOPALTG_ARA1','IM219EIOPALTG_ARA1','SM119EIOPALTG_ARA1']} – damadam Dec 4 '19 at 11:41
  • if I'm wrong, please edit your question by giving us the expected result – damadam Dec 4 '19 at 11:47
2

An alternative leaving the last "entry":

sed -E 's/LINK:.*,(.+$)/[\1/' file

Output:

...
LINK:['SM119EIOPALTG_ARA1']}
1

This works:

$ 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:[]}

Breaking down s/LINK:\[.*]/LINK:[]/g:

  • s/LINK:\[.*\] search for everything beginning with LINK:[ and ending with ]. Because [ is a search control characters we must prefix it with \.
  • /LINK:[] replace everything found with this.
  • /g Not positive but I think g is for greedy and means do it more than once.
  • Note that this will delete everything until the last ] on the line. So if a line is LINK[foo] bar, baz whatever [nope], then you would lose everything, not only what's inside the first [ ]. You might want to use sed 's/LINK:\[[^]]*/LINK:[/g' file instead. – terdon Dec 4 '19 at 12:09
  • @terdon I was going by the implication that } was end of record marker and LINK: was the last field group on the record. Your comment is an excellent caveat for readers who do not have the same file format as OP. OP still needs to clarify what exactly output should look like so I can still fine-tune answer after that. Besides it's time to go to work :) – WinEunuuchs2Unix Dec 4 '19 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.