Thursday, March 23, 2017

bash: sed - part II

The w substitution flag produces the same output but stores the output in the specified file (test.txt).
The normal out of sed editor appears in STDOUT, but only the lines that include the matching pattern are stored in the specified output file.


$ sed 's/test/trial/w test.txt' data5.txt
This is a trial line.
This is a different line


$ cat test.txt
This is a trial line.




===============================================
The sed editor modified the text in line two per address specified:


$ sed '2s/dog/cat/' data1a.txt
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog

===============================================
Here's another example, this time using a range of line addresses:


$ sed '2,3s/dog/cat/' data1a.txt
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy dog

===============================================

If you want to apply a command to a group of lines starting at some point within the text, but continuing to the end of the test. Use the $ sign:


$ sed '2,$s/dog/cat/' data1a.txt
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat

No comments:

Post a Comment