sed [options] commands [file-to-edit]We will copy some files into our home directory to practice some editing.
cd cp /usr/share/common-licenses/BSD . cp /usr/share/common-licenses/GPL-3 .Let's use sed to view the contents of the BSD license file we copied.
sed '' BSD
Copyright (c) The Regents of the University of California. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. ... ...This works because the single quotes contain the editing commands we are passing to sed. We passed it nothing, so it just printed each line it received to standard output.
cat BSD | sed ''
Copyright (c) The Regents of the University of California. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. . . . . . .As you can see, we can operate on files or streams of text (as is produced when piping output with the "|" character) just as easily.
sed 'p' BSD
Copyright (c) The Regents of the University of California. Copyright (c) The Regents of the University of California. All rights reserved. All rights reserved. Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions modification, are permitted provided that the following conditions are met: are met: . . . . . .You can see that sed has printed each line twice now. This is because it automatically prints each line, and then we've told it to print explicitly with the "p" command.
sed -n 'p' BSD
Copyright (c) The Regents of the University of California. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. . . . . . .We now are back to printing each line once.
sed -n '1p' BSD
Copyright (c) The Regents of the University of California.By placing the number "1" before the print command, we have told sed the line number to operate on.
sed -n '1,5p' BSD
Copyright (c) The Regents of the University of California. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditionsWe've just given an address range to sed. If we give sed an address, it will only perform the commands that follow on those lines. In this example, we've told sed to print line 1 through line 5.
sed -n '1,+4p' BSDThis will result in the same output, because we've told sed to start at line 1 and then operate on the next 4 lines as well.
sed -n '1~2p' BSD
Copyright (c) The Regents of the University of California. modification, are permitted provided that the following conditions 1. Redistributions of source code must retain the above copyright 2. Redistributions in binary form must reproduce the above copyright documentation and/or other materials provided with the distribution. may be used to endorse or promote products derived from this software . . . . . .
sed '1~2d' BSD
All rights reserved. Redistribution and use in source and binary forms, with or without are met: notice, this list of conditions and the following disclaimer. notice, this list of conditions and the following disclaimer in the 3. Neither the name of the University nor the names of its contributors without specific prior written permission. . . . . . .It is important to note here that our source file is not being affected. It is still intact. The edits are output to our screen.
sed '1~2d' BSD > everyother.txt
sed -i '1~2d' everyother.txt
sed -i.bak '1~2d' everyother.txt
's/old_word/new_word/'The "s" is the substitute command. The three slashes (/) are used to separate the different text fields. You can use other characters to delimit the fields if it would be more helpful.
echo "http://www.example.com/index.html" | sed 's_com/index_org/home_'
http://www.example.org/home.htmlDo not forget the final delimiter, or sed will complain.
echo "http://www.example.com/index.html" | sed 's_com/index_org/home'
sed: -e expression #1, char 22: unterminated `s' commandLet's create a file to practice our substitutions on:
echo "this is the song that never ends yes, it goes on and on, my friend some people started singing it not knowing what it was and they'll continue singing it forever just because..." > annoying.txtNow let's substitute the expression "on" with "forward".
sed 's/on/forward/' annoying.txt
this is the sforwardg that never ends yes, it goes forward and on, my friend some people started singing it not knowing what it was and they'll cforwardtinue singing it forever just because...You can see a few notable things here. First, is that we are replacing patterns, not words. The "on" within "song" is changed to "forward".
sed 's/on/forward/g' annoying.txt
this is the sforwardg that never ends yes, it goes forward and forward, my friend some people started singing it not knowing what it was and they'll cforwardtinue singing it forever just because...Now the substitute command is changing every instance.
sed 's/on/forward/2' annoying.txt
this is the song that never ends yes, it goes on and forward, my friend some people started singing it not knowing what it was and they'll continue singing it forever just because...If we only want to see which lines were substituted, we can use the "-n" option again to suppress automatic printing.
sed -n 's/on/forward/2p' annoying.text
yes, it goes on and forward, my friendAs you can see, we can combine the flags at the end of the command.
sed 's/SINGING/saying/i' annoying.txt
this is the song that never ends yes, it goes on and on, my friend some people started saying it not knowing what it was and they'll continue saying it forever just because...
sed 's/^.*at/REPLACED/' annoying.txt
REPLACED never ends yes, it goes on and on, my friend some people started singing it REPLACED it was and they'll continue singing it forever just because...You can see that the wildcard expression matches from the beginning of the line to the last instance of "at".
sed 's/^.*at/(&)/' annoying.txt
(this is the song that) never ends yes, it goes on and on, my friend some people started singing it (not knowing what) it was and they'll continue singing it forever just because...A more flexible way of referencing matched text is to use escaped parentheses to group sections of matched text.
sed 's/\([a-zA-Z0-9][a-zA-Z0-9]*\) \([a-zA-Z0-9][a-zA-Z0-9]*\)/\2 \1/' annoying.txt
is this the song that never ends yes, goes it on and on, my friend people some started singing it knowing not what it was they and'll continue singing it forever because just...As you can see, the results are not perfect. For instance, the second line skips the first word because it has a character not listed in our character set. Similarly, it treated "they'll" as two words in the fifth line.
sed 's/\([^ ][^ ]*\) \([^ ][^ ]*\)/\2 \1/' annoying.txt
is this the song that never ends it yes, goes on and on, my friend people some started singing it knowing not what it was they'll and continue singing it forever because... justThis is much better than last time. This groups punctuation with the associated word.
Article ID: 208
Created On: Fri, Dec 27, 2013 at 3:11 AM
Last Updated On: Fri, Dec 27, 2013 at 3:11 AM
Authored by: ASPHostServer Administrator [asphostserver@gmail.com]
Online URL: http://faq.asphosthelpdesk.com/article.php?id=208