To delete everything between square brackets

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite
1












I need to find the line in a lvm.conf file which starts with string, global_filter and remove everything between the square brackets except "r/.*/".



There is only 1 line which starts with global_filter.



Before Removal:



global_filter = [ "a|^/dev/sda.*$|", "a|^/dev/sdb.*$|", "r/.*/"]


After the Removal, it should be -



global_filter = [, "r/.*/"]









share|improve this question























  • Okay, I suppose you know how to address a line in sed and you know the s command, so the only problem is to define a good regex for your match? How about [.*,? Now build your command or ask a specific question.
    – Philippos
    Jul 24 '17 at 11:20










  • Should global_filter lines that do not contain "r/.*/" be modified?
    – Stéphane Chazelas
    Jul 24 '17 at 11:27











  • Are you sure you want to leave the leading comma in?
    – Stéphane Chazelas
    Jul 24 '17 at 11:28










  • Yes I want the comma should be retained. And also only that line in the file should be modified.
    – user7290726
    Jul 24 '17 at 12:43














up vote
0
down vote

favorite
1












I need to find the line in a lvm.conf file which starts with string, global_filter and remove everything between the square brackets except "r/.*/".



There is only 1 line which starts with global_filter.



Before Removal:



global_filter = [ "a|^/dev/sda.*$|", "a|^/dev/sdb.*$|", "r/.*/"]


After the Removal, it should be -



global_filter = [, "r/.*/"]









share|improve this question























  • Okay, I suppose you know how to address a line in sed and you know the s command, so the only problem is to define a good regex for your match? How about [.*,? Now build your command or ask a specific question.
    – Philippos
    Jul 24 '17 at 11:20










  • Should global_filter lines that do not contain "r/.*/" be modified?
    – Stéphane Chazelas
    Jul 24 '17 at 11:27











  • Are you sure you want to leave the leading comma in?
    – Stéphane Chazelas
    Jul 24 '17 at 11:28










  • Yes I want the comma should be retained. And also only that line in the file should be modified.
    – user7290726
    Jul 24 '17 at 12:43












up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I need to find the line in a lvm.conf file which starts with string, global_filter and remove everything between the square brackets except "r/.*/".



There is only 1 line which starts with global_filter.



Before Removal:



global_filter = [ "a|^/dev/sda.*$|", "a|^/dev/sdb.*$|", "r/.*/"]


After the Removal, it should be -



global_filter = [, "r/.*/"]









share|improve this question















I need to find the line in a lvm.conf file which starts with string, global_filter and remove everything between the square brackets except "r/.*/".



There is only 1 line which starts with global_filter.



Before Removal:



global_filter = [ "a|^/dev/sda.*$|", "a|^/dev/sdb.*$|", "r/.*/"]


After the Removal, it should be -



global_filter = [, "r/.*/"]






linux sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 22:25









Rui F Ribeiro

38.2k1475125




38.2k1475125










asked Jul 24 '17 at 11:11









user7290726

348




348











  • Okay, I suppose you know how to address a line in sed and you know the s command, so the only problem is to define a good regex for your match? How about [.*,? Now build your command or ask a specific question.
    – Philippos
    Jul 24 '17 at 11:20










  • Should global_filter lines that do not contain "r/.*/" be modified?
    – Stéphane Chazelas
    Jul 24 '17 at 11:27











  • Are you sure you want to leave the leading comma in?
    – Stéphane Chazelas
    Jul 24 '17 at 11:28










  • Yes I want the comma should be retained. And also only that line in the file should be modified.
    – user7290726
    Jul 24 '17 at 12:43
















  • Okay, I suppose you know how to address a line in sed and you know the s command, so the only problem is to define a good regex for your match? How about [.*,? Now build your command or ask a specific question.
    – Philippos
    Jul 24 '17 at 11:20










  • Should global_filter lines that do not contain "r/.*/" be modified?
    – Stéphane Chazelas
    Jul 24 '17 at 11:27











  • Are you sure you want to leave the leading comma in?
    – Stéphane Chazelas
    Jul 24 '17 at 11:28










  • Yes I want the comma should be retained. And also only that line in the file should be modified.
    – user7290726
    Jul 24 '17 at 12:43















Okay, I suppose you know how to address a line in sed and you know the s command, so the only problem is to define a good regex for your match? How about [.*,? Now build your command or ask a specific question.
– Philippos
Jul 24 '17 at 11:20




Okay, I suppose you know how to address a line in sed and you know the s command, so the only problem is to define a good regex for your match? How about [.*,? Now build your command or ask a specific question.
– Philippos
Jul 24 '17 at 11:20












Should global_filter lines that do not contain "r/.*/" be modified?
– Stéphane Chazelas
Jul 24 '17 at 11:27





Should global_filter lines that do not contain "r/.*/" be modified?
– Stéphane Chazelas
Jul 24 '17 at 11:27













Are you sure you want to leave the leading comma in?
– Stéphane Chazelas
Jul 24 '17 at 11:28




Are you sure you want to leave the leading comma in?
– Stéphane Chazelas
Jul 24 '17 at 11:28












Yes I want the comma should be retained. And also only that line in the file should be modified.
– user7290726
Jul 24 '17 at 12:43




Yes I want the comma should be retained. And also only that line in the file should be modified.
– user7290726
Jul 24 '17 at 12:43










2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










sed -i -e '/^global_filter/' data


Explanation



sed -i -e '
# look at only lines that begin with global_filter
/^global_filter/"r/.*/"
' data





share|improve this answer






















  • Thanks for the help. This replaces the entire file. I want only that line to be modified.
    – user7290726
    Jul 24 '17 at 12:44










  • I modified the command by replacing the -n flag by -i and it worked. It is working as expected but I am seeing a duplicate entry the next line.
    – user7290726
    Jul 24 '17 at 12:54











  • Can you please help me in that.
    – user7290726
    Jul 24 '17 at 12:55










  • Yes I tried with the first method itself. It is giving duplicate entry in the file.
    – user7290726
    Jul 24 '17 at 13:03










  • With -n option, the entire file is being replaced by just the global_filter line.
    – user7290726
    Jul 24 '17 at 13:14

















up vote
0
down vote













Try this:



$ echo 'global_filter = [ "a|^/dev/sda.$|", "a|^/dev/sdb.$|", "r/.*/"]'|
sed -E 's/(^global_filter = [)(.*)(, "r.*$)/13/'


Output:



global_filter = [, "r/.*/"]





share|improve this answer






















  • Just try on the input: global_filter = [ "a|^/dev/sda.$|", "a|^/dev/sdb.$|", "r/Dot_Star/"]'
    – user218374
    Jul 24 '17 at 11:24










  • Sorry John this is not working
    – user7290726
    Jul 24 '17 at 13:04











  • I mean for file input, I could not get this working.. can you help me out.
    – user7290726
    Jul 24 '17 at 13:15










  • @user7290726 I see. Could you please than post an excerpt of your input file to make it easier to guess what you want?
    – John Goofy
    Jul 24 '17 at 13:20










  • The file has only 1 line which starts with global_filter. And I need the changes as said earlier.
    – user7290726
    Jul 26 '17 at 2:55










Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f381396%2fto-delete-everything-between-square-brackets%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










sed -i -e '/^global_filter/' data


Explanation



sed -i -e '
# look at only lines that begin with global_filter
/^global_filter/"r/.*/"
' data





share|improve this answer






















  • Thanks for the help. This replaces the entire file. I want only that line to be modified.
    – user7290726
    Jul 24 '17 at 12:44










  • I modified the command by replacing the -n flag by -i and it worked. It is working as expected but I am seeing a duplicate entry the next line.
    – user7290726
    Jul 24 '17 at 12:54











  • Can you please help me in that.
    – user7290726
    Jul 24 '17 at 12:55










  • Yes I tried with the first method itself. It is giving duplicate entry in the file.
    – user7290726
    Jul 24 '17 at 13:03










  • With -n option, the entire file is being replaced by just the global_filter line.
    – user7290726
    Jul 24 '17 at 13:14














up vote
1
down vote



accepted










sed -i -e '/^global_filter/' data


Explanation



sed -i -e '
# look at only lines that begin with global_filter
/^global_filter/"r/.*/"
' data





share|improve this answer






















  • Thanks for the help. This replaces the entire file. I want only that line to be modified.
    – user7290726
    Jul 24 '17 at 12:44










  • I modified the command by replacing the -n flag by -i and it worked. It is working as expected but I am seeing a duplicate entry the next line.
    – user7290726
    Jul 24 '17 at 12:54











  • Can you please help me in that.
    – user7290726
    Jul 24 '17 at 12:55










  • Yes I tried with the first method itself. It is giving duplicate entry in the file.
    – user7290726
    Jul 24 '17 at 13:03










  • With -n option, the entire file is being replaced by just the global_filter line.
    – user7290726
    Jul 24 '17 at 13:14












up vote
1
down vote



accepted







up vote
1
down vote



accepted






sed -i -e '/^global_filter/' data


Explanation



sed -i -e '
# look at only lines that begin with global_filter
/^global_filter/"r/.*/"
' data





share|improve this answer














sed -i -e '/^global_filter/' data


Explanation



sed -i -e '
# look at only lines that begin with global_filter
/^global_filter/"r/.*/"
' data






share|improve this answer














share|improve this answer



share|improve this answer








edited Jul 24 '17 at 13:15

























answered Jul 24 '17 at 11:21







user218374


















  • Thanks for the help. This replaces the entire file. I want only that line to be modified.
    – user7290726
    Jul 24 '17 at 12:44










  • I modified the command by replacing the -n flag by -i and it worked. It is working as expected but I am seeing a duplicate entry the next line.
    – user7290726
    Jul 24 '17 at 12:54











  • Can you please help me in that.
    – user7290726
    Jul 24 '17 at 12:55










  • Yes I tried with the first method itself. It is giving duplicate entry in the file.
    – user7290726
    Jul 24 '17 at 13:03










  • With -n option, the entire file is being replaced by just the global_filter line.
    – user7290726
    Jul 24 '17 at 13:14
















  • Thanks for the help. This replaces the entire file. I want only that line to be modified.
    – user7290726
    Jul 24 '17 at 12:44










  • I modified the command by replacing the -n flag by -i and it worked. It is working as expected but I am seeing a duplicate entry the next line.
    – user7290726
    Jul 24 '17 at 12:54











  • Can you please help me in that.
    – user7290726
    Jul 24 '17 at 12:55










  • Yes I tried with the first method itself. It is giving duplicate entry in the file.
    – user7290726
    Jul 24 '17 at 13:03










  • With -n option, the entire file is being replaced by just the global_filter line.
    – user7290726
    Jul 24 '17 at 13:14















Thanks for the help. This replaces the entire file. I want only that line to be modified.
– user7290726
Jul 24 '17 at 12:44




Thanks for the help. This replaces the entire file. I want only that line to be modified.
– user7290726
Jul 24 '17 at 12:44












I modified the command by replacing the -n flag by -i and it worked. It is working as expected but I am seeing a duplicate entry the next line.
– user7290726
Jul 24 '17 at 12:54





I modified the command by replacing the -n flag by -i and it worked. It is working as expected but I am seeing a duplicate entry the next line.
– user7290726
Jul 24 '17 at 12:54













Can you please help me in that.
– user7290726
Jul 24 '17 at 12:55




Can you please help me in that.
– user7290726
Jul 24 '17 at 12:55












Yes I tried with the first method itself. It is giving duplicate entry in the file.
– user7290726
Jul 24 '17 at 13:03




Yes I tried with the first method itself. It is giving duplicate entry in the file.
– user7290726
Jul 24 '17 at 13:03












With -n option, the entire file is being replaced by just the global_filter line.
– user7290726
Jul 24 '17 at 13:14




With -n option, the entire file is being replaced by just the global_filter line.
– user7290726
Jul 24 '17 at 13:14












up vote
0
down vote













Try this:



$ echo 'global_filter = [ "a|^/dev/sda.$|", "a|^/dev/sdb.$|", "r/.*/"]'|
sed -E 's/(^global_filter = [)(.*)(, "r.*$)/13/'


Output:



global_filter = [, "r/.*/"]





share|improve this answer






















  • Just try on the input: global_filter = [ "a|^/dev/sda.$|", "a|^/dev/sdb.$|", "r/Dot_Star/"]'
    – user218374
    Jul 24 '17 at 11:24










  • Sorry John this is not working
    – user7290726
    Jul 24 '17 at 13:04











  • I mean for file input, I could not get this working.. can you help me out.
    – user7290726
    Jul 24 '17 at 13:15










  • @user7290726 I see. Could you please than post an excerpt of your input file to make it easier to guess what you want?
    – John Goofy
    Jul 24 '17 at 13:20










  • The file has only 1 line which starts with global_filter. And I need the changes as said earlier.
    – user7290726
    Jul 26 '17 at 2:55














up vote
0
down vote













Try this:



$ echo 'global_filter = [ "a|^/dev/sda.$|", "a|^/dev/sdb.$|", "r/.*/"]'|
sed -E 's/(^global_filter = [)(.*)(, "r.*$)/13/'


Output:



global_filter = [, "r/.*/"]





share|improve this answer






















  • Just try on the input: global_filter = [ "a|^/dev/sda.$|", "a|^/dev/sdb.$|", "r/Dot_Star/"]'
    – user218374
    Jul 24 '17 at 11:24










  • Sorry John this is not working
    – user7290726
    Jul 24 '17 at 13:04











  • I mean for file input, I could not get this working.. can you help me out.
    – user7290726
    Jul 24 '17 at 13:15










  • @user7290726 I see. Could you please than post an excerpt of your input file to make it easier to guess what you want?
    – John Goofy
    Jul 24 '17 at 13:20










  • The file has only 1 line which starts with global_filter. And I need the changes as said earlier.
    – user7290726
    Jul 26 '17 at 2:55












up vote
0
down vote










up vote
0
down vote









Try this:



$ echo 'global_filter = [ "a|^/dev/sda.$|", "a|^/dev/sdb.$|", "r/.*/"]'|
sed -E 's/(^global_filter = [)(.*)(, "r.*$)/13/'


Output:



global_filter = [, "r/.*/"]





share|improve this answer














Try this:



$ echo 'global_filter = [ "a|^/dev/sda.$|", "a|^/dev/sdb.$|", "r/.*/"]'|
sed -E 's/(^global_filter = [)(.*)(, "r.*$)/13/'


Output:



global_filter = [, "r/.*/"]






share|improve this answer














share|improve this answer



share|improve this answer








edited Jul 24 '17 at 11:50

























answered Jul 24 '17 at 11:21









John Goofy

556113




556113











  • Just try on the input: global_filter = [ "a|^/dev/sda.$|", "a|^/dev/sdb.$|", "r/Dot_Star/"]'
    – user218374
    Jul 24 '17 at 11:24










  • Sorry John this is not working
    – user7290726
    Jul 24 '17 at 13:04











  • I mean for file input, I could not get this working.. can you help me out.
    – user7290726
    Jul 24 '17 at 13:15










  • @user7290726 I see. Could you please than post an excerpt of your input file to make it easier to guess what you want?
    – John Goofy
    Jul 24 '17 at 13:20










  • The file has only 1 line which starts with global_filter. And I need the changes as said earlier.
    – user7290726
    Jul 26 '17 at 2:55
















  • Just try on the input: global_filter = [ "a|^/dev/sda.$|", "a|^/dev/sdb.$|", "r/Dot_Star/"]'
    – user218374
    Jul 24 '17 at 11:24










  • Sorry John this is not working
    – user7290726
    Jul 24 '17 at 13:04











  • I mean for file input, I could not get this working.. can you help me out.
    – user7290726
    Jul 24 '17 at 13:15










  • @user7290726 I see. Could you please than post an excerpt of your input file to make it easier to guess what you want?
    – John Goofy
    Jul 24 '17 at 13:20










  • The file has only 1 line which starts with global_filter. And I need the changes as said earlier.
    – user7290726
    Jul 26 '17 at 2:55















Just try on the input: global_filter = [ "a|^/dev/sda.$|", "a|^/dev/sdb.$|", "r/Dot_Star/"]'
– user218374
Jul 24 '17 at 11:24




Just try on the input: global_filter = [ "a|^/dev/sda.$|", "a|^/dev/sdb.$|", "r/Dot_Star/"]'
– user218374
Jul 24 '17 at 11:24












Sorry John this is not working
– user7290726
Jul 24 '17 at 13:04





Sorry John this is not working
– user7290726
Jul 24 '17 at 13:04













I mean for file input, I could not get this working.. can you help me out.
– user7290726
Jul 24 '17 at 13:15




I mean for file input, I could not get this working.. can you help me out.
– user7290726
Jul 24 '17 at 13:15












@user7290726 I see. Could you please than post an excerpt of your input file to make it easier to guess what you want?
– John Goofy
Jul 24 '17 at 13:20




@user7290726 I see. Could you please than post an excerpt of your input file to make it easier to guess what you want?
– John Goofy
Jul 24 '17 at 13:20












The file has only 1 line which starts with global_filter. And I need the changes as said earlier.
– user7290726
Jul 26 '17 at 2:55




The file has only 1 line which starts with global_filter. And I need the changes as said earlier.
– user7290726
Jul 26 '17 at 2:55

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f381396%2fto-delete-everything-between-square-brackets%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?