Unix Sed Tutorial: Append, Insert, Replace, and Count File Lines

by Sasikala on November 9, 2009

Linux Sed Examples - File Manipulation CommandsThis article is part of the on going Unix sed command tutorial series. In our previous articles we learned sed with single commands — printing, deletion, substitute and file write.

Sed provides lot of commands to perform number of operations with the lines in a file.

In this article let us review how to append, insert, replace a line in a file and how to get line numbers of a file.

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

$cat thegeekstuff.txt
Linux Sysadmin
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Windows- Sysadmin, reboot etc.

Append Lines Using Sed Command

Sed provides the command “a” which appends a line after every line with the address or pattern.

Syntax:

#sed 'ADDRESS a\
	Line which you want to append' filename

#sed '/PATTERN/ a\
	Line which you want to append' filename

Sed Append Example 1. Add a line after the 3rd line of the file.

Add the line “Cool gadgets and websites” after the 3rd line. sed “a” command inserts the line after match.

$ sed '3 a\
> Cool gadgets and websites' thegeekstuff.txt

Linux Sysadmin
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Cool gadgets and websites
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Windows- Sysadmin, reboot etc.

Sed Append Example 2. Append a line after every line matching the pattern

The below sed command will add the line “Linux Scripting” after every line that matches the pattern “Sysadmin”.

$ sed '/Sysadmin/a \
> Linux Scripting' thegeekstuff.txt

Linux Sysadmin
Linux Scripting
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Windows- Sysadmin, reboot etc.
Linux Scripting

Sed Append Example 3. Append a line at the end of the file

The following example, appends the line “Website Design” at the end of the file.

$ sed '$ a\
> Website Design' thegeekstuff.txt

Linux Sysadmin
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Windows- Sysadmin, reboot etc.
Website Design

Insert Lines Using Sed Command

Sed command “i” is used to insert a line before every line with the range or pattern.

Syntax:

#sed 'ADDRESS i\
	Line which you want to insert' filename

#sed '/PATTERN/ i\
	Line which you want to insert' filename

Sed Insert Example 1. Add a line before the 4th line of the line.

Add a line “Cool gadgets and websites” before 4th line. “a” command inserts the line after match whereas “i” inserts before match.

$ sed '4 i\
> Cool gadgets and websites' thegeekstuff.txt

Linux Sysadmin
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Cool gadgets and websites
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Windows- Sysadmin, reboot etc.

Sed Insert Example 2. Insert a line before every line with the pattern

The below sed command will add a line “Linux Scripting” before every line that matches with the pattern called ‘Sysadmin”.

$ sed '/Sysadmin/i \
> Linux Scripting' thegeekstuff.txt

Linux Scripting
Linux Sysadmin
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Linux Scripting
Windows- Sysadmin, reboot etc.

Sed Insert Example 3. Insert a line before the last line of the file.

Append a line “Website Design” before the last line of the file.

$ sed '$ i\
> Website Design' thegeekstuff.txt

Linux Sysadmin
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Website Design
Windows- Sysadmin, reboot etc.

Replace Lines Using Sed Command

“c” command in sed used to replace every line matches with the pattern or ranges with the new given line.

Syntax:

#sed 'ADDRESS c\
	new line' filename

#sed '/PATTERN/ c\
	new line' filename

Sed Replace Example 1. Replace a first line of the file

The below command replaces the first line of the file with the “The Geek Stuff”.

$ sed '1 c\
> The Geek Stuff' thegeekstuff.txt

The Geek Stuff
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Windows- Sysadmin, reboot etc.

Sed Replace Example 2. Replace a line which matches the pattern

Replace everyline which has a pattern “Linux Sysadmin” to “Linux Sysadmin – Scripting”.

$ sed '/Linux Sysadmin/c \
> Linux Sysadmin - Scripting' thegeekstuff.txt

Linux Sysadmin - Scripting
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Windows- Sysadmin, reboot etc.

Sed Replace Example 3. Replace the last line of the file

Sed command given below replaces the last line of the file with “Last Line of the file”.

$ sed '$ c\
> Last line of the file' thegeekstuff.txt

Linux Sysadmin
Databases - Oracle, mySQL etc.
Security (Firewall, Network, Online Security etc)
Storage in Linux
Productivity (Too many technologies to explore, not much time available)
Last line of the file

Print Line Numbers Using Sed Command

“=” is a command in sed to print the current line number to the standard output.

Syntax:

#sed '=' filename

The above send command syntax prints line number in the first line and the original line from the file in the next line .

sed ‘=’ command accepts only one address, so if you want to print line number for a range of lines, you must use the curly braces.

Syntax:

# sed -n '/PATTERN/,/PATTERN/ {
=
p
}' filename

Sed Line Number Example 1. Find the line number which contains the pattern

The below sed command prints the line number for which matches with the pattern “Databases”

$ sed -n '/Databases/=' thegeekstuff.txt

2

Sed Line Number Example 2. Printing Range of line numbers

Print the line numbers for the lines matches from the pattern “Oracle” to “Productivity”.

$ sed -n '/Oracle/,/Productivity/{
> =
> p
> }' thegeekstuff.txt

2
Databases - Oracle, mySQL etc.
3
Security (Firewall, Network, Online Security etc)
4
Storage in Linux
5
Productivity (Too many technologies to explore, not much time available)

Sed Line Number Example 3. Print the total number of lines in a file

Line number of the last line of the file will be the total lines in a file. Pattern $ specifies the last line of the file.

$ sed -n '$=' thegeekstuff.txt

6

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

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

{ 27 comments… read them below or add one }

1 linuxindetails November 12, 2009 at 3:31 am

very nice tutorial !
With Debian version of sed, you need to use the option ‘-i’ to really append lines in your file.By default, the modification appears on the standard output.

2 Ramesh Natarajan December 10, 2009 at 6:02 pm

@linuxindetails,

Thanks for clarifying how the option -i works on Debian.

3 Balast July 3, 2010 at 12:29 pm

Hi,

How to insert content of file1 on the top of the file2, file3, file4, etc in same directory ?

4 Manish November 4, 2010 at 8:53 am

Hi

How I can escape Back slashes in sed. I want to change the pattern or string
from
allow ^127\.0\.0\.1$
to
allow ^192\.168\.1\.10$

Thank You,
Manish

5 frankhuang November 17, 2010 at 2:25 am

To balast:
for f in `ls file[2-4].txt` ; do
cat file1.txt $f > newfile
cp newfile $f
done

To Manish:
sed ‘s/127\\\.0/192\\\.168/g’ filename
works

6 Anonymous December 1, 2010 at 3:19 am

@ Blast

cat file2>> file1 should work fine

7 Balaji May 6, 2011 at 3:25 am

Very nice tutorial…
But how to write these sed commands in a script… do they work only in command-line?

8 David Liontooth June 18, 2011 at 9:26 am

Thanks! I nominate for the best set of sed instructions on the Internet.

9 David Liontooth June 18, 2011 at 9:28 am

Balaji — they all work in scripts; see here for instance

10 mary July 29, 2011 at 3:14 pm

I want to change the first line of a csv file into lower case. How to do that. thanks

11 arvind January 12, 2012 at 7:49 am

I want to add “something else” after “something” , so i use the below code.
sed ‘
/something/ a\
something else
‘ test.txt
This will just list the contents,but how do i write this new line to the file. I mean the same file and not to a new file.

12 macmadness86 April 11, 2012 at 9:33 am

I agree with arvind, how do you write the changes to the original file. In other words, make the input also the output after sed function has been applied. that was a pun..ha

13 phamming May 11, 2012 at 5:37 am

@Anonymous
cat file1>>file2 only works when the text of file1 may be appended (added to the bottom) to file2.
@Balast
Maybe something like
for f in file[2-4];do sed 1r$f file1>$f;done
could work. It is important not to add quotes around file[2-4], else only the contents of file1 are written to the another files.
for f in file[2-4];do cat file1 $f>$f;done
could also work.
Be careful to first test the strings, because on other computers, it can work different from mine.

14 Steve August 27, 2012 at 8:20 pm

sed -i -e ‘s|foo|bar|’ file.text

Note the “-i” is for in-place. For safety it will make a backup if you supply an extension to -i, as in “-i.bak”.

15 shridhara November 2, 2012 at 4:11 am

sed ’1 c\
> The Geek Stuff’ thegeekstuff.txt

this command doesn’t affect the original file.

help me

16 Carlos Luna November 3, 2012 at 12:33 pm

Hi Shridhara,

I am trying to achieve something similar only solution so far for me is

echo “The Geek Stuff” | sed ’1a\’ >> thegeekstuff.txt

It should be something cleaner tough.Anyhow hope it helps.

Carlos

17 Steve Bennett February 5, 2013 at 6:17 pm

Argh, the formatting in this article is so confusing. There shouldn’t be any linefeed after the backslashes. So it’s not this:

sed ‘ADDRESS i\
Stuff to add’ filename

It’s this:

sed ‘ADDRESS i\Stuff to add’ filename.

Otherwise you get this error:
sed: -e expression #1, char 32: extra characters after command

18 madxkatz February 19, 2013 at 1:56 am

For those who are on SunOS which is non-GNU, the following code will help to insert a line at top of file, to delete first line or delete last line:

sed ’1i\^J
line to add’ test.dat > tmp.dat

sed ’1d’ test.dat > tmp.dat

sed ‘$d’ tmp.dat > test.dat

^J is inserted with ^V+^J
Add the newline after ’1i.
\ MUST be the last character of the line.
The second part of the command must be in a second line.

19 Nidhi March 29, 2013 at 12:09 am

i want to channge a file with contents like

}
some text here

want to remove the new line after }. so the result should be
}some text here

how to do it using sed

20 Reynold May 18, 2013 at 12:44 pm

Is variable substitution supported with the sed insert command ?

I tried to insert the public IP as the first line in /etc/hosts , but its not working
eg:

publicIP=$(curl -s checkip.dyndns.org|egrep -o ‘[0-9\.]+’ 2>/dev/null)
sed “1 i\${publicIP} heuristics” /etc/hosts

Any idea ?

21 Holger June 28, 2013 at 3:30 am

For inserting lines from the commandline I use the “s” command.
E.g. inserting “the very first line” to the file geek.txt do

sed “1s/^/the very first line\n/”

My problem with the a and i command (working at the commandline) is, that you cannot define the END of the command. So everything following it will be printed – and usually you want to do multiple commands… (though I’m working on a windows-machine, maybe for unix/linux there is a better solution)

22 sanjay August 29, 2013 at 5:37 am

How to used the sed append, insert functions in a script to be excuted on remote server. For example I want the command below to be excuted on a remote server using ssh. (solaris)

$ sed ‘$ i\
> Website Design’ thegeekstuff.txt

23 Pat October 23, 2013 at 3:51 am

Very clean and helpful tutorial. Thanks!

24 igor November 26, 2013 at 1:37 pm

Very good tutorial!
But how can i append something at the end of a matched line?
Thanks

25 Bob Donahue December 12, 2013 at 8:40 am

When I try the append examples it doesn’t add a newline to the end of the appended line.

If test.txt is:
LINE1
FOO
LINE3

sed ‘/FOO/ a
BAR’ test.txt

gives

LINE1
FOO
BARLINE3

26 Anonymous December 20, 2013 at 10:04 am

Makes it easy to understand :)

27 igor December 20, 2013 at 2:26 pm

@ bob:
sed ‘/FOO/s/$/BAR/’ test.txt
#substitute the end of the line with BAR

Leave a Comment

Previous post:

Next post: