Writing to files – the w command

The sed command for writing is w. Using this command, we can write lines from one file to another file.

Here is an example:

    $ cat new.txt 
  

The output is as follows:

    new is a empty file
    
    $ sed -n '/Chicken/w new.txt' shopping.txt
    $ cat new.txt 
    Chicken    3    5    15
  

After the w command, we specify the file to which we will perform the write operation. In this example, the line containing the Chicken pattern is written to the new.txt file.

Get Learning Linux Shell Scripting - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.