≡ Menu

Unix Sed Tutorial: How To Write to a File Using Sed

Sed Examples for writing to a file on LinuxThis article is part of Unix Sed Tutorial series. In previous articles, we discussed about sed print operation , sed delete operation and sed find and replace.

In this article, let us review how to extract part of one file and write it to another file using sed.

Sed provides “w” command to write the pattern space data to a new file.

Sed creates or truncates the given filename before reads the first input line and it writes all the matches to a file without closing and re-opening the file.

Syntax: 

#sed 'ADDERSSw outputfile' inputfilename

#sed '/PATTERN/w outputfile' inputfilename

Sed reads a line and place it in a pattern buffer and writes the pattern buffer to the given output file according to the supplied commands.

Let us first create thegeekstuff.txt file that will be used in all the examples mentioned below.

# cat thegeekstuff.txt
1. Linux - Sysadmin, Scripting etc.
2. Databases - Oracle, mySQL etc.
3. Hardware
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)
8. Website Design
9. Software Development
10.Windows- Sysadmin, reboot etc.

Let us review some examples of write command in sed.

1. Write 1st line of the file

In this example, 1 (address) refers the first line of the input and w writes the pattern buffer to the output file “output.txt”

$ sed -n '1w output.txt' thegeekstuff.txt

$ cat output.txt
1. Linux - Sysadmin, Scripting etc.

2. Write first & last line of the file

In this example, 1 and $ refers first and last line respectively.

$ sed -n -e '1w output.txt' -e '$w output.txt' thegeekstuff.txt

$ cat output.txt
1. Linux - Sysadmin, Scripting etc.
10.Windows- Sysadmin, reboot etc.

3. Write the lines matches with the pattern Storage or Sysadmin

In this example sed command writes the lines which matches the pattern “Storage” or “Sysadmin”.

$ sed -n -e '/Storage/w output.txt' -e '/Sysadmin/w output.txt' thegeekstuff.txt

$ cat output.txt
1. Linux - Sysadmin, Scripting etc.
5. Storage
10.Windows- Sysadmin, reboot etc.

4. Write the lines from which the pattern matches to till end of the file

In this example, /Storage/,$ represents line matches from Storage to end of the file.

$ sed -n '/Storage/,$w output.txt' thegeekstuff.txt

$ cat output.txt
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)
8. Website Design
9. Software Development
10.Windows- Sysadmin, reboot etc.

5. Write the lines which matches pattern and next two lines from match

In this example, the send command writes the line matches for “Storage” and two lines next to that.

$ sed -n '/Storage/,+2w output.txt' thegeekstuff.txt

$ cat output.txt
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • M.Sivagami July 21, 2011, 7:19 am

    Hi,
    May i know how to substitute a \n with multiple new lines….
    Thanks in advance…

  • ali July 29, 2012, 5:22 am

    thanks for perfect and aesy understanding sed tutorial.

  • Sreenu May 22, 2013, 3:11 am

    Below command is not working
    $ sed -n ‘/Storage/,+2w output.txt’ thegeekstuff.txt

    I tried like – sed -n ‘/0393888778/,+2w output.txt’ Req.txt
    giving me below error.
    sed: command garbled: /0393888778/,+2w output.txt

  • Paul May 22, 2013, 3:11 am

    Below command is not working
    $ sed -n ‘/Storage/,+2w output.txt’ thegeekstuff.txt

    I tried like – sed -n ‘/0393888778/,+2w output.txt’ Req.txt
    giving me below error.
    sed: command garbled: /0393888778/,+2w output.txt

  • Raj May 22, 2013, 3:23 am

    I have a scenario like,
    Needs to content between 2 strings(Begin and End),after End i have some more lines i also needs to print next 4 lines from End.
    example:- (both Ganesh and Rajesh are unique id’s -> i wants to print from Ganesh to till } curly brackets, with the help of Unique Id’s together only i needs to get the output , below i have created 2 lines of script, how to combine both , like a recursive , how to do that ?)

    sed -n ‘/Ganesh/,/Rajesh/p’ Teq.txt
    sed -n ‘/Rajesh/10p,’ Teq.txt

    Teq.txt
    Names
    {
    Ganesh
    Mahesh
    Rajesh
    Suresh
    {
    Tippu Suresh
    Nalla Suresh
    }
    }
    Colors
    {
    Red
    White
    Blue
    }

  • Mahesh Vakharia November 7, 2014, 8:45 am

    I would lie if you can correct following shell.
    I like to print lines between 10 to 20 from a file “abc”.
    I prefer flexible , user can give the line numbers from and to , for selection from a file.
    test.v
    echo -n “Enter the input file name :”
    read a
    echo -n “Enter the output file name:”
    read b
    echo -n “Enter the FROM Line number:”
    read c
    echo -n ” Enter the TO Line number :”
    read d

    sed -n ‘$c,$d p’ $a > $b
    —————————————-
    Please guide where I am making mistake?
    With due Regards.

    Mahesh Vakharia

  • John February 19, 2017, 6:41 pm

    Thank for a clear explanation with examples