How to modify files with SED?

12 May 2014 Written by 
Rate this item
(0 votes)
How to modify files with SED? photo credit: adesigna via photopin cc

What is sed?

Sed is a stream editor which is used to perform basic text transformations on an input stream (a file or input from a pipeline) or filter strings. It permits to modify the contents of different lines in a file, based on some parameters.

For the examples shown in this article, you used the 4.2.1 version of sed as shown below:

sed --version

GNU sed version 4.2.1
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
to the extent permitted by law.

Sed has a syntax as follows:

sed [-options] [command] [<file(s)>]

 

Visualization

For the cases where you need to display the contents of a file or part of it, we can use any of the following commands:

sed 5q file1                              # see the firsts 5 lines of file1
cat -n file1 | sed -n '5,6 p'        # see lines 5 and 6 of file1
sed -n '1p' file1 > file2             # copy the first line of file1 to file2
sed -n '$p' file1                        # show the last line of file1

 

Replacing strings and characters

In these cases, it is always advisable storing in another file, the results of a substitution, leaving the original file unchanged. Some examples are:

# replace strings in all lines that satisfied the string of file1 and store the result in file2
sed 's/old_string/new_string/g' file1 > file2
# replace strings only in lines 200 y 201
sed '200,201 s/old_string/new_string/g' file1 > file2
# replace several strings by new one
sed 's/old_string_1\|old_string_2/new_string/g' file1 > file2
# replace all lowercase to uppercase
sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' file1 > file2

 

Insert strings

As in the previous case, the original file is left unchanged while the second will contain the changes.

# insert a string at the beginning of each line
sed 's/^/string_at_the_beginning/' file1 >file2
# insert a string at the end of the file
sed -e '$ string_at_the_end' file > file2
# insert a blank line before each line that matches with string
sed '/string/{x;p;x;}' file1 > file2
# insert a blank line after each line that matches with string
sed '/string/G' file1 > file2
# insert a blank line before and other after each line that matches with string
sed '/string/{x;p;x;G;}' file1 > file2
# insert a blank line every 2 lines
sed 'n;G;' file1 > file2

 

Delete lines and strings

If we want to delete characters or entire lines, either because they are empty or commented, we can use the following commands:

sed '2,4 d' file1 > file2                    # remove lines 100 and 105 of file1
sed '5,20 !d' file1 > file2                 # delete all lines except the 5 and 20
sed '$d' file1 > file2                        # delete the last line of file1
sed -i '$d' file1                               # delete the last line of file1 in the same file
sed '/string/ d' file1 > file2              # remove lines that satisfied a string
sed '/^$/d' file1 > file2                    # remove blank lines
sed '/^$/d; / *#/d' file1 > file2          # remove blank lines and bash comments

 

11020 Last modified on Thursday, 22 September 2016 19:43
More in this category:
Luis Sequeira

Luis Sequeira is an IT professional with experience in cloud environments, quality of service and network traffic analysis, who loves looking for solutions to engineering challenges, share knowledge. At work, the main challenge is to integrate different network and software technologies to provide solution in a wide range of areas, e.g., virtual network functions, machine learning, autonomous driving, robotics and augmented reality.

Website: https://www.luissequeira.com

12 comments

  • guaranteed weight loss Comment Link
    guaranteed weight loss 25 May 2015, 06:56
    I’m not that much of a online reader to be honest but your
    blogs really nice, keep it up! I'll go ahead and bookmark your site to come back later on. Cheers
  • Local SEO Toronto Comment Link
    Local SEO Toronto 10 June 2015, 16:13
    In fact no matter if someone doesn't know then its up to other
    users that they will assist, so here it takes place.
  • Developed by Digital Shift Comment Link
    Developed by Digital Shift 22 July 2015, 16:11
    This is my first time pay a quick visit at here and i am actually pleassant to read all
    at single place.
  • IT Consultant Comment Link
    IT Consultant 27 July 2015, 14:16
    Hi! I could have sworn I've been to your blog
    before but after looking at some of the posts I realized it's
    new to me. Regardless, I'm definitely happy I came across
    it and I'll be bookmarking it and checking back often!
  • u Comment Link
    u 24 November 2015, 04:15
    It's actually a nice and helpful piece of info. I'm glad that you simply shared this useful info with us.
    Please stay us up to date like this. Thank you for sharing.
  • www.ussharemarkets.com Comment Link
    www.ussharemarkets.com 10 December 2015, 00:19
    Hi all, here every one is sharing these kinds of know-how, thus
    it's good to read this blog, and I used to pay a quick visit this website every day.
  • reviews flooring Comment Link
    reviews flooring 13 December 2015, 17:13
    Good post however I was wondering if you could write a litte more on this subject?

    I'd be very grateful if you could elaborate a little bit further.
    Bless you!
  • Leticia Alcazar Comment Link
    Leticia Alcazar 04 March 2016, 17:50
    Great piece of information.
    I was wondering if you have more post like this!
  • intralinks dataroom login Comment Link
    intralinks dataroom login 24 March 2016, 02:00
    Hi my loved one! I want to say that this article is
    amazing, great written and include approximately all vital infos.

    I'd like to look extra posts like this .
  • angry baboons Comment Link
    angry baboons 12 May 2016, 17:36
    I just could not go away your website before suggesting that I
    extremely enjoyed the usual info a person supply in your guests?
    Is gonna be back frequently in order to check out new posts

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.