We have talked about the g flag, with which we can change every string that matches the pattern line by line instead of the first one. Now Let me introduce other flags to you.
examples:
1.echo "This is a test"; echo "Today is a good day." | sed -r 's;([a-zA-Z]+day) is a ([a-zA-Z]+) (day);\1 and yesterday are \2 \3s;g'
2.echo "This is a test"; echo "Today is a good day." | sed -r 's;([a-zA-Z]+day) is a ([a-zA-Z]+) (day);\1 and yesterday are \2 \3s;gp'
3.echo "This is a test"; echo "Today is a good day." | sed -n -r 's;([a-zA-Z]+day) is a ([a-zA-Z]+) (day);\1 and yesterday are \2 \3s;gp'
By default, sed prints every line. If it makes a substitution, the new text is printed instead of the old one.[example 1]
Notice the third example, if you use an optional argument to sed, "sed -n," it will not, by default, print any new lines. [example 3]
We can use '>>' or '>' to write the results to a file or some devices. But if you’re not in a shell. This way may not be a good idea. But do not worry. Sed has it’s own way. There’s a flag ---- '/w'.
example:
1. echo "Today is a good day." | sed -r 's/.*good.*/& &/gw doubledSentence'
This command will double any sentence that cantains good and write the results to a file.
As you can see, we can combine the flags together without any problem. But '/w' has to be the last flag.

浙公网安备 33010602011771号