Answer the question
In order to leave comments, you need to log in
How to find and replace the second occurrence?
Hello.
hello.txt
hello: ""
hello: ""
hello: ""
hello: ""
hello: "world"
hello: ""
sed -i "s/hello: \"\"/hello: \"world\"/" hello.txt
sed -i "s/hello: \"\"/hello: \"world\"/g" hello.txt
Answer the question
In order to leave comments, you need to log in
Something like this:
, where the colon is the definition of a label named a ;
N - add a new line to pattern space ;
$ - to the last line;
! - apply;
b - jump to label, in this case label a ;
s - search and replace;
/""/ - what we are looking for, in this case, empty quotes;
"world" - what to replace the found match with, in this case, "world" ;
/2 - replacement of the second match.
echo "hello: \"\"
hello: \"\"
hello: \"\"" | sed ':a;N;$!ba;s/""/"world"/2'
sed '2s/""/"world"/1'
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question