0

How to uncomment the below lines efficiently from my config file.

#PPD=/home/mine/ppd (Line no. 100)
#_dco_ppd.c \
#       -DUSE_LIC -I$(LIC)/include -I$(LIC)/include/grok.c \
#       -L$(LIC)/lib -llic -lokta

I tried below options none worked

grep -A 4 "PPD=/home/mine/ppd" config | xargs 0 sed -i 's/^#//g' [threw below error]

sed: invalid option -- 'D' Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

Other tries

sed -i 's|#PPD=/home/mine/ppd|PPD=/home/mine/ppd|' config (worked)
sed -i 's|#_dco_ppd.c|_dco_ppd.c|' config (worked)
sed -i 's|#       -DUSE_LIC|       -DUSE_LIC|' config (didnt work)
sed -i 's|#-DUSE_LIC|-DUSE_LIC|' config (didnt work as well)

Where am I going wrong ?

I'm on 14.04.1-Ubuntu Docker container

GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)

Inian
  • 80,270
  • 14
  • 142
  • 161
Goku
  • 482
  • 1
  • 7
  • 22
  • when I try your attempt, `0` after `xargs` will throw `No such file or directory` error (but you got some other error).. in any case, you cannot pass grep results to sed in such a manner when you need to use inplace as well – Sundeep Nov 23 '20 at 10:51

1 Answers1

3
sed -Ei '/^#PPD/,/^#.*-lokta$/{s/#//}' configfile

Look for the text required in your sed statement

/^#PPD/ - line beginning with (^) #PPD to (signified by ,) ...

/^#.*-lokta$/ - line beginning with (^) #, then anything, then -lokta, end of line ($)

Then execute the substitution enclosed within {}

Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18
  • 2
    Good one. suggestion: `-E` and `{}` grouping isn't needed here and `s/^#//` would be more robust – Sundeep Nov 23 '20 at 10:49
  • 1
    Fair point but I think curly braces help readability. – Raman Sailopal Nov 23 '20 at 10:52
  • 2
    agree. another suggestion would be `sed '/^#PPD/,+3 s/^#//'` since number of lines seems to be fixed.. and OP likely has `GNU sed` based on `-i` usage and `Ubuntu Docker` – Sundeep Nov 23 '20 at 10:53
  • @RamanSailopal : Wow, worked like a charm. Can you please explain once the purpose of ,/^#.* – Goku Nov 23 '20 at 10:53
  • 1
    @Goku see https://stackoverflow.com/questions/38972736/how-to-print-lines-between-two-patterns-inclusive-or-exclusive-in-sed-awk-or – Sundeep Nov 23 '20 at 10:54
  • 1
    @SUndeep : Appreciate your help and time :) thanks for the suggestion – Goku Nov 23 '20 at 11:00