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/.*/"]
  • 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
sed -i -e '/^global_filter/{\|"r/\.\*/"|s|.*|global_filter = [, "r/.*/"]|;}' data


sed -i -e '
   # look at only lines that begin with global_filter
      # and also they should contain the string "r/.*/"
      # change everything to the following expression
      \|"r/\.\*/"| s|.*|global_filter = [, "r/.*/"]|

      ## alternatively could be written:
#global_filter = [, "r/.*/"]
' data
  • 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

Try this:

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


global_filter = [, "r/.*/"]
  • 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

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.