Issue
I have a file like below.
# Start OF Java_Out_Of_Memory
- displayName: "Java_Out_Of_Memory"
logDirectory: "/opt/xyz"
logName: "TextLog_*"
searchStrings:
- displayName: "Out_Of_Memory"
pattern: "java.lang.OutOfMemoryError"
matchExactString: false
caseSensitive: false
# End OF Java_Out_Of_Memory
I wanted to add line exactly below of caseSensitive: false with printMatchesString: false...
# Start OF Java_Out_Of_Memory
- displayName: "Java_Out_Of_Memory"
logDirectory: "/opt/xyz"
logName: "TextLog_*"
searchStrings:
#displayName Should be unique across the patterns including the case.
- displayName: "Out_Of_Memory"
pattern: "java.lang.OutOfMemoryError"
matchExactString: false
caseSensitive: false
printMatchedString: false
# End OF Java_Out_Of_Memory
I don't know how many lead space of matched string... Leading space may vary depends on file to file.
I tried this
sed -i '/^[^#]*caseSensitive:*/a \\ printMatchedString: false'
So my requirement is to add a line exactly below and even consider lead space
NOTE:- I have multi configuration like above and I need add PrinitMatched string on every configuration
Solution
Try this:
sed -E '/^[^#]*caseSensitive:/ {p; s/^([[:blank:]]*)[^[:blank:]].*$/\1printMatchesString: false/;}' myfile.txt
Explanation:
/^[^#]*caseSensitive:/
- on lines matching "caseSensitive:" (not preceded by "#"{
- do the following block of commandsp
- print the current line (in its original form)s/^([[:blank:]]*)[^[:blank:]].*$/\1printMatchesString: false/
- parse the line as a bunch of blanks (in parens, so it's a capture group) and then whatever's left, and replace it with the blanks (\1
recalls the capture group) and "printMatchesString: false". Note that the result will be printed automatically.}
- marks the end of the group of commands
Answered By - Gordon Davisson Answer Checked By - Robin (WPSolving Admin)