0

I have text file, and in it has a block that has pattern like below:

# cat file
Jit .... enabled=false
Jit ..shoes.. From=2021-01-01
Jit ..gloves.. From=2021-01-01

so I want to change all of these matching regex, wrote a sed file with these lines.

# cat sedfile
/^Jit/ {
s/enabled=false/enabled=true/g
s/From=\d+-\d+-\d+/From=2021-02-01/g

}

Pattern /^Jit/ greps lines only starting with Jit and other steps do substitutions. Date can be anything, so I can't hardcode these, therefore matching a pattern is required.

Desired Ouput:

Jit .... enabled=false
Jit ..shoes.. From=2021-01-01
Jit ..gloves.. From=2021-01-01
Sollosa
  • 1,929
  • 4
  • 20
  • 38
  • @schrodigerscatcuriosity apology, I made corrections – Sollosa Oct 05 '21 at 08:02
  • Thanks! It seems that you wouldn't need the greedy g flag, since you are performing one substitution per line. What should be the desired output? – schrodingerscatcuriosity Oct 05 '21 at 08:05
  • @schrodigerscatcuriosity global is required, because there are plenty date patterns and all, but I can solve it's complexity, if I get to be sure that pattern is workin with sed substitution, right now it isn't. – Sollosa Oct 05 '21 at 08:08
  • Then please [edit] your question and show us an example that accurately represents your file. If there can be more than one date per line, that is a completely different situation. – terdon Oct 05 '21 at 08:10
  • 1
    Also, what's wrong with your existing solution? The sed script you show should work well if you replace the \d with [0-9] since sed doesn't understand \d. – terdon Oct 05 '21 at 08:13
  • @terdon ok I updated. [...] symbolizes more text actually, and it doesn't matter in my scenario. – Sollosa Oct 05 '21 at 08:16
  • 2
    It always matters. What if you have the word not-enabled=true? Should that be changed? But anyway, since you still only show one pattern per line, it looks like you don't need g which is only useful if you want to make multiple substitutions on the same line. – terdon Oct 05 '21 at 08:18

2 Answers2

2

Sed doesn't know about \d. Just use the standard [0-9] character class instead:

$ cat sedfile
/^Jit/ {
s/enabled=false/enabled=true/
s/From=[0-9]+-[0-9]+-[0-9]+/From=2021-02-01/

}

Then you can do:

line1
Jit .... enabled=false
Jit .... From=2021-01-01
line4

$ sed -Ef sedfile file line1 Jit .... enabled=true Jit .... From=2021-02-01 line4

Or, without a script file:

$ sed -E '/^Jit/{s/enabled=false/enabled=true/; s/From=[0-9]+-[0-9]+-[0-9]+/From=2021-02-01/}' file
line1
Jit .... enabled=true
Jit .... From=2021-01-01
line4

I removed the g modifier since you only seem to have one occurrence of the pattern per line.

terdon
  • 242,166
1

I propose a solution assuming (until more comprehensive sample input) that the lines have strict end patterns. If that's the case it wouldn't be necessary more complex matching.

/^Jit/{
  s/false$/true/
  s/[0-9\-]\+$/2021-02-01/
}'

Output:

Jit .... enabled=true
Jit .... From=2021-02-01

About the removed g flag, see @terdon's answer.