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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.