My need is to append a new string after the first /> for all files in a directory.Here is my script:#!/bin/bashfind ./ -type f -print0 | while IFS= read -r -d '' myfile; do    sed -i '0,/\/>/asome text here' $myfiledoneHowever, it seems that all of \n are matched too. For example if a file is like this:abcdef<abc/><abc/>After executing my script, I'll get the result as below:abcsome text heredefsome text here<abc/>some text here<abc/>It does append the new string for the first occurrence of />, but it appends the new string for each line before the first /> too.
How to search '/>' with sed
linux;sed
Yes. The expression 0,/regex/ will cause the command following it (a) to be applied to all lines up to (and including) the first match of the regular expression.My guess is that you instead would want to dosed -i '0,/regex/{//a text}' $myfileThis will select all lines up to the first match of the regular expression, and then apply the command only on the line within that range that matches the expression, while passing all other lines through unchanged.The literal newline is needed to signal the end of the text that you add.