A stream editor is used to perform basic text 13 transformations on an input stream (a file or input from a pipeline). We want to match the first occurrence only with sed. Regular Expression HOWTO where it will match even a newline. {} Explicit quantifier notation. Insert a line after first pattern match via sed stream editor. >sed '/unix/ a "Add a new line"' file. Slightly Friendlier Syntax: Go-sed is a little more user-friendly when it comes to syntax. You say "I want to target the pattern #: (a number followed by a colon) and add a new line after" when a more accurate description would be: replace the space before any number of digits followed by a colon with a newline. What is sed. If str is a single piece of text (either a character vector or a string scalar), then newStr is also a single piece of text of the same type. Department of Energy is using GAMS in two projects aimed at advancing carbon capture technologies. -z to also match newline with \n and -o to only print what matches the If you don't mind using sed, here is a possible. Introduction; Syntax overview; Help; Autosuggestions. One is the command mode and another is the insert mode. # insert_line_if_no_match (regex, newline) ⇒ Object search the file line by line and match each line with the given regex if not matched, insert newline at the end of the file. You can write. The above worked when I tried it with GNU sed. So, the lines will be added to the file AFTER the line where condition matches. For example, say I got: #output databases: mysql, snort, password yourpassword, more, blah and after this line I'd like to add:. sed: match string and append new line (before or after) admin. sed '/public class/i\ @XmlRootElement(name="ABC") ' file > outFile Note, the blank line after your new inserted text. sed operates by performing the following cycle on each line of input: First, sed reads one line from the input stream, removes any trailing newline, and places it in the pattern space. Best How To : As alk said, if you use CLONE_THREAD you can not use wait() to wait for your thread to finish. sed -e 's/00/11/3' -e 's/0/1/12' test. Let us consider a file with the following contents: $ cat file Linux Solaris Fedora Ubuntu AIX HPUX Inserting before the. The traditional Unix utility sed makes it possible to manipulate strings and streams of text from the command line without using a text editing application. I have a bunch of files in a folder For all the files here is what I want to do: Insert ". Contribute to zherczeg/pcresp development by creating an account on GitHub. Love to work with Linux. A zero-length extension means that the backup has the same name as the new file, so no new file is created. what appears after the circumflex must appear at the beginning of the pattern space. To create a backup file prior to editing, add the backup extension directly after the "-i" option: sed -i. SED/AWK - Add to the Beginning. , like CLONE_PARENT), so that calls to getppid(2) return the same value for all of the threads in a thread group. This is another input. Insert a line with 'i' You can insert a new line before the pattern with the "i" command:. Both are initially empty. arkit learners. – Brad Nov 25 '15 at 21:15. Here is awk syntax: awk '/regex/ { getline. However, sed does add a newline when you ask it to perform certain commands. Our rental pool is mixed. sed '$ new text' myfile sed: 0602-404 Function $ new text cannot be parsed What did I get wrong?. ASCII is the American Standard Code for Information Interchange. But, I want to match a given string or a regular pattern expression and display on the screen. In addition, GNU sed 4. *)$/ \1\/p>/p' rime. trying to use se | The UNIX and Linux Forums. sed '0~3 s/$/\nMy Text/g' < input. I've written my first perl script to deal with converting a dump file (from a piece of equipment) to something MathCAD can import. To insert a line, type it like this: sed '4iThis is the inserted line. (Perhaps Mr. NOTE: This command will only add one line of text before the final line, not a file. By default sed does not terminate if there is no 'next. sed -e 's/00/11/3' -e 's/0/1/12' test. I am trying to add a new line of text after finding a particular string of text in a file. If your file is small (and /etc/profile should be) awk can edit it in situ, and it can leave the file alone if there is no match. Saturday, June 24, 2017 Linux Tips and Tricks, sed. “a” command inserts the line after match whereas “i” inserts before match. To answer the question as asked, you'd have to do sed 's/pattern. Note the two single quotes after the \n. Add a line “Cool gadgets and websites” before 4th line. sed: Insert multiple lines before or after pattern match add multiple lines after a pattern is matched in a newline which can be before or after the match based. hi all,i have a text file and i want to add a new line to it, i want to add the new line after/before a certain linei dont want to add a line at the 4th row or 3rd row as this c add a line after pattern matching - Linux Forum - Spiceworks. I would probably use awk which has a string-match function called index(). Add a line before the 4th line of the line. The above worked when I tried it with GNU sed. Selecting lines with SED. Ask Question Asked 5 months ago. */&\n/', otherwise you'll insert the newline right after the match instead of at the end of the line. I'd like to be able to add a new line after matching text in a line. A token is a character, special character, character class, or range (e. txt [insert line after every line with matching patter] sed '2 i this is sed insert test' somedata. To insert after, you want "a" (append). Viewed 1k times 2. A zero-length extension means that the backup has the same name as the new file, so no new file is created. SED Whole Word Match with Boundaries Linux Shell. This episode contains another 10 command examples covering things such as deleting source code's comments , converting image files (along with ImageMagick tool), and inserting a single or multiple lines to a file. Note that some versions of sed require that an extension be specified with -i. When an input command, such as a (append), i (insert) or c (change), is given, ed enters input mode. Stackoverflow. cpp /* * * Copyright (c) 1998-2002 * Dr John Maddock * * Use, modification and distribution are subject to the * Boost Software License. I’d like to insert a newline after dot. Any character in a string contained in 'single quotes' has no special meaning. This tutorial covers advanced use, tagging, vim plugins and integration with cscope. awk solution:. According. txt [insert line after every line with matching patter] sed '2 i this is sed insert test' somedata. – Brad Nov 25 '15 at 21:15. These examples deal with finding doubled occurrences of words in a document. done exit # Exercises: # ----- # 1) The script usually inserts a blank line at the end #+ of the target file. How do I insert a newline into the RHS of a substitution using sed under Linux / UNIX like operating systems? My line pattern is as follows: This is a test. If you use sed you have to fork/exec an entirely new process (very expensive for something that the shell can do internally). Insert a line after the matched string. Addresses in a SED script can be in any of the following forms: `number' Specifying a line number will match only that line in the input. While in some ways similar to an editor which permits scripted edits, SED works by making only one pass over the input(s), and is consequently more efficient. Sed, so called because it is a stream editor, is perfect for applying a series of edits to a number of files. #comment The comment extends until the next newline (or the end of a -e script fragment). - pattern must match a complete line - insert after pattern a new line with new_pattern Sed in my installed Ubuntu does not seem to accept /c or /a commands, only -i !!. Why? This is because the default behavior of sed is to print every line after parsing it. Sed stands for stream editor. collection of linux one-liner commands. Note that some versions of sed require that an extension be specified with -i. libs/regex/test/regress/parse. Stack Exchange network consists of 176 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. Insert string to the begining of lines: sed -i 's/^/head /g' my-file: Insert string to the end of lines: sed -i 's/$/ tail/g' my-file: Add content after nth line: sed -n -i 'p;3a "new string"' my-file, cat -n my-file Link: unix. By default, newline is a completely ordinary character with no special meaning. GNU sed also supports some special 2-address forms: 0,addr2 Start out in "matched first address" state, until addr2 is found. sed: match string and append new line (before or after) admin. First is c which states that everything except for the selected character are to be translated. 0~30 will insert every 30 th line. -B num print numlines of leading context before matching lines. It removes the need to run rm after doing an in-place replace. We want to match the first occurrence only with sed. I prefer the first form because it's easier to add a new command by adding a new line and because the intent is clearer. The -i '' (or, in Linux, just -i) tells sed to use a zero-length extension for the backup. unix is opensource. hi all,i have a text file and i want to add a new line to it, i want to add the new line after/before a certain linei dont want to add a line at the 4th row or 3rd row as this c add a line after pattern matching - Linux Forum - Spiceworks. Since newline is just another character when stored internally, a regular expression can use "\n" to match an embedded newline. what appears after the circumflex must appear at the beginning of the pattern space. The target audience is a Bash programmer who knows the basics of sed and wants to learn the rest. Perl One-liner. According. How do I insert a newline into the RHS of a substitution? Several versions of sed permit '\n' to be typed directly into the RHS, which is then converted to a newline on output: ssed, gsed302a+, gsed103 (with the -x switch), sed15+, sedmod, and UnixDOS sed. sed is useful in a number of different contexts, including finding and replacing strings of text in a large number of files, manipulating text for Stack Scripts and other kinds of scripts, as well as a component in basic shell scripting. So, the lines will be added to the file AT the location where line number matches. Beginning at the first line in the file, grep copies a line into a. The Linux operating system makes use of terminal commands to work with a computer's file system. We want to automate the build generation using Jenkins and our URL Scheme can be changed for various builds, so we want to modify the Info. This will search forward through the file for searchstring. Let's start by looking at searches and doing search and replace operations within Vim. txt is hello linux ; Nodes this is how can i append the text after pattern match using variable in sed. The ! here inverts the pattern match. 3 (stable) BusyBox 1. unix is opensource. Insert string to the begining of lines: sed -i 's/^/head /g' my-file: Insert string to the end of lines: sed -i 's/$/ tail/g' my-file: Add content after nth line: sed -n -i 'p;3a "new string"' my-file, cat -n my-file Link: unix. # insert department name after each title and keep backup. #comment The comment extends until the next newline (or the end of a -e script fragment). Sed Insert Newline After Match. txt like this: lots of text lots of text #nvram = [ # these # could # be, # anything #] lots of text lots of text and I want to add some text "foobar" to get this:. The first and last are quoted, and the middle is not. 2, I want to: Match Line1 Check Line2, just below Line1, if it equals String If it doesn't - append String after Line1 (or insert before Line2) What I tried: echo -e "Line1\. Sed Interview Questions : In my previous article i have explained about Sed command with real examples. A zero-length extension means that the backup has the same name as the new file, so no new file is created. It removes the need to run rm after doing an in-place replace. -B num print numlines of leading context before matching lines. sed - Unix, Linux Command Manual Pages (Manpages) , Learning fundamentals of UNIX and Linux in simple and easy steps : A beginner's tutorial containing complete knowledge of Unix Korn and Bourne Shell and Programming, Utilities, File System, Directories, Memory Management, Special Variables, vi editor, Processes. 1 in the Installation Guide for a description of the tape drives available on your system. Add a double blank lines after each line sed G test. Here is an example: inserting a new line after pattern: Input file: foo Art street #pattern foo pattern color Output: foo Art street #pattern foo pattern NEW LINE HERE color. ' _will_ match a newline in the middle of the pattern space. Now if we want to add a new line with some content & that too before a pattern match, than we can use option 'i' with sed command & pattern to be searched, $ sed '/dan i "Adding text before match" ' test. I have a requirement where I have to insert a new line with some data at a particular line. Hi I just wanted to add a new line after every matching pattern: The method doing this doesn't matter, however, I have been using sed and this is what I tried doing, knowing that I am a bit off: sed 'Wf a\'/n'/g' Basically, I want to add a new line after occurrence of Wf. keep learning, keep growing CentOS inserting new line of text after another using sed not working Because. hi all,i have a text file and i want to add a new line to it, i want to add the new line after/before a certain linei dont want to add a line at the 4th row or 3rd row as this c add a line after pattern matching - Linux Forum - Spiceworks. Inserting commas into numbers with sed. In a previous blog post I demonstrated how to use sed to insert text before or after a line in many files, and in this example I'd like to demonstrate how to delete a range of lines using sed. This is a pattern This is a pattern I can add a new line with the sed command. Didn't you just ask a similar question about changing strings using sed? This one perhaps: answer to How do I change "Apple book" to "an Apple tree"?. Stack Exchange Network. done exit # Exercises: # ----- # 1) The script usually inserts a blank line at the end #+ of the target file. - mklement0 Jun 6 '16 at 2:29. txt > out. txt, but I would. You can have sed add lines to the output stream, either before or after lines that match the address. Let us consider a file. unix is free os. It removes the need to run rm after doing an in-place replace. I want to add a new line after I search for number 64 in a file. You can add a line in the same way as the examples above sed '4aThis is the appended line. Here -n option inhibits printing, first pattern deletes all the lines with /debug/, and the second command forces printing of some of the remaining lines (which match /foo/) Append, insert, and change line sed command in unix examples: 6) To insert , we use option i. Security (Firewall, Network, Online Security etc) Cool gadgets and websites Storage in Linux Productivity (Too many. /regexp/ Match lines matching the regular expression regexp. I know that this my be really simple, but I'm having a hard time accomplishing it. 35+ Examples of Regex Patterns Using sed and awk in Linux It provides more patterns like matching digits, and words. Been looking around for this but haven't been able to find anything. Hi I just wanted to add a new line after every matching pattern: The method doing this doesn't matter, however, I have been using sed and this is what | The UNIX and Linux Forums. The "a" command to sed tells it to add a new line after a match is found. The modifier causes ^ and $ to match respectively (in addition to the normal behavior) the empty string after a newline, and the empty string before a newline. This section uses N and D commands to search for consecutive words spanning multiple lines. Go-sed eats whitespace until it sees the filename. Insert new line or text after every n lines in Linux In one of our earlier articles, we discussed how to insert a line before or after a pattern. Insert a carriage return before a line feed, and presto, a CR/LF ends each line. A stream editor is used to perform basic text 13 transformations on an input stream (a file or input from a pipeline). ' _will_ match a newline in the middle of the pattern space. Regular Expression HOWTO where it will match even a newline. Append and Insert. awk solution:. In this article, we will see the different ways in which we can insert a line before or after on finding a pattern. The full documentation for sed is maintained as a Texinfo manual. txt Linux is a Unix-like computer operating system. Hi, I have a file which contains many occurances of a string say "hellosunil". The \n sequence matches a new-line character in the pattern space, except the terminating new-line character. One is the command mode and another is the insert mode. unix is free os. I would like to insert new string at last line staring with some text using sed, [closed] insert the whole match newline to the end of the file, if the match. Sed Insert Newline After Match. bak files created by sed that are copies of the original unchanged files. Whenever sed is executed on an input file or on the contents from stdin, sed reads the file line-by-line and after removing the trailing newline, places it in the "Pattern space", where the commands are executed on them after conditions (as in case of regex matching) are verified, and then printed on the stdout. OPERANDS The following operand is supported: file If the file argument is given, ed will simulate an e command on the file named by the pathname, file, before accepting commands from the standard input. sed and awk notes. The sed command includes many features for selecting lines to be modified and making changes only to the selected lines. In a normal sed, you have to use one (and ONLY one) space between a r or w and the filename. When -z is used, a zero byte (the ascii 'NUL' character) is added between the lines (instead of a new line). Finding doubled words in a single line is easy using GNU grep and similarly with GNU sed:. Changing mode from one to another. Saturday, June 24, 2017 Linux Tips and Tricks, sed. Note the the space between the dollar sign and the i and the backslash (\) after the i. info Linux Shell Scripting CookbookSolve real-world shell scripting problems with over 110 simple but. I have successfully used regexp and sed to insert a newline before or after a line containing a matched pattern /WORD/. POSIX sed requires a \ and a newline after the a sed function. q [exit-code] Immediately quit the sed script without processing any more input, except that if auto-print is not disabled the current pattern space will be printed. is the pattern string and the second is the string which I need to insert once I have that pattern. I have following file and I want to insert a line at 4th line as "This is my 4th line". See what GNU sed does (taken from the info page):. When you want something inserted after a line, you can move the command {print} or switch to : sed '/Insert command output after this line/r'<(ls -l) text. The last three parameters are optional. NOTE: This command will only add one line of text before the final line, not a file. Finding doubled words in a single line is easy using GNU grep and similarly with GNU sed:. I'd like to be able to add a new line after matching text in a line. Drop in a comment if you know how to. In this article, we will see the different ways in which we can insert a line before or after on finding a pattern. For example, say I got: #output databases: mysql, snort, password yourpassword, more, blah and after this line I'd like to add:. Insert new line or text after every n lines in Linux In one of our earlier articles, we discussed how to insert a line before or after a pattern. The "a" command to sed tells it to add a new line after a match is found. So this will match empty lines and proceed to. Add a line after a match. com sed "a" command lets us append lines to a file, based on the line number or regex provided. You can use the backreferences \1 through \9 in the replacement text to re-insert text matched by a capturing group. I'm trying to take the contents of a file and insert it after a matching pattern in another file using sed. file In a file with six lines you get something like the following: Line 1 Line 2 Line 3 My Text Line 4 Line 5 Line 6 My Text Just swap the second half of 0~3 with the number of lines after which you want to insert to adjust where you insert your text, e. sed: match string and append new line (before or after) admin. Insert line after first match using sed. Regular expressions match entire strings, not just individual lines, but a NEWLINE character is matched by \n in a sed RE. When you want something inserted after a line, you can move the command {print} or switch to : sed '/Insert command output after this line/r'<(ls -l) text. I created a sed script that cleans up the the file (there's lots of "dirty" formatting like double spaces and spaces before/after commas). Hi I just wanted to add a new line after every matching pattern: The method doing this doesn't matter, however, I have been using sed and this is what I tried doing, knowing that I am a bit off: sed 'Wf a\'/n'/g' Basically, I want to add a new line after occurrence of Wf. trying to use se | The UNIX and Linux Forums Inserting new line after match of a fixed string The UNIX and Linux Forums. sed 'n;G' man sed explains ~ as: first ~ step Match every step'th line starting with line first. txt [insert line after every line with matching patter] sed '2 i this is sed insert test' somedata. It was written in 1973-4 by Lee E. I wanted to find the text called “foo” and replaced to “bar” in the file named “hosts. This Unix command is used for command line processing. Been looking around for this but haven't been able to find anything. Now that we're writing sed scripts in separate files, we can take advantage of the append, insert, and change line commands. Since you know how to use grep to find the lines to be deleted, using grep -v and the same pattern will give you all the lines to be kept. Next, here's a sed script I used to insert two HTML "div" tags, the first one after the opening body tag, and the second one before the closing body tag. So, the lines will be added to the file AFTER the line where condition matches. I know that this my be really simple, but I'm having a hard time accomplishing it. 1 in the Installation Guide for a description of the tape drives available on your system. It removes the need to run rm after doing an in-place replace. Adding a Line with sed •sed can be used to add a line of text to a file •The format for this sed instruction is /STRING/a\ LINE_OF_TEXT •The \ turns off the special meaning of the newline which is to signal to the shell that you are done with the command •This command will add the line of text after every line that contains STRING. Contribute to fniessen/refcard-org-beamer development by creating an account on GitHub. Below are the various commands for text manipulation. , like CLONE_PARENT), so that calls to getppid(2) return the same value for all of the threads in a thread group. com: Add a new line to 3th line: sed -i "3i mynew string" my-file: Insert a line of text before a line. This occurs, as you will see in the next chapter, in the special case when another line is appended to the current line in the pattern space. $ sed '3 a\ > Cool gadgets and websites' thegeekstuff. Here is awk syntax: awk '/regex/ { getline. Now if we want to add a new line with some content & that too before a pattern match, than we can use option 'i' with sed command & pattern to be searched, $ sed '/dan i "Adding text before match" ' test. share | improve this question. awk solution:. Use the a command to append (write after the selected line) and the i command to insert (write before the selected line). preserves the literal value of the next character. Matches any character, including newline. `^#include' will match only lines where "#include" is the first thing on line, but if there are one or two spaces before, the match fail $ the same as ^, but refers to end of line \c matches character `c. 2, I want to: Match Line1 Check Line2, just below Line1, if it equals String If it doesn't - append String after Line1 (or insert before Line2) What I tried: echo -e "Line1\. However, my new rule would disappear after a system restart. Well, more specifically, typing a newline is the only way to match a newline character when entering a regexp interactively (as there is no regexp escape sequence for a newline), and C-q C-j is the most reliable way to type a newline at a prompt. info Linux Shell Scripting CookbookSolve real-world shell scripting problems with over 110 simple but. Finding doubled words in a single line is easy using GNU grep and similarly with GNU sed:. If you use sed you have to fork/exec an entirely new process (very expensive for something that the shell can do internally). txt Check the difference in the examples above. In a normal sed, you have to use one (and ONLY one) space between a r or w and the filename. Whenever sed is executed on an input file or on the contents from stdin, sed reads the file line-by-line and after removing the trailing newline, places it in the "Pattern space", where the commands are executed on them after conditions (as in case of regex matching) are verified, and then printed on the stdout. So this will match empty lines and proceed to. Use the a command to append (write after the selected line) and the i command to insert (write before the selected line). search the file line by line and match each line with the given regex if matched, insert newline after each matching line. 10 Easy Examples of GNU sed Command Lines (Episode 5) This is the fifth episode of our GNU sed command examples series. Contribute to zherczeg/pcresp development by creating an account on GitHub. However, at times, we need to print a few more lines following the lines matching the pattern. Some Sed implementations, like GNU Sed, makes the newline after the initial backslash optional, even when forced in --posix mode. */&\n/', otherwise you'll insert the newline right after the match instead of at the end of the line. I was working on certain project last week where i need to insert line after match in 100 of files in Linux or Unix operating system. Look at the above result properly, the line Gmail got printed twice. The M modifier to regular-expression matching is a GNU sed extension which causes ^ and $ to match respectively (in addition to the normal behavior) the empty string after a newline, and the empty string before a newline. Can be used to search, substitute, delete, append files. "Add a new line" learn operating system. Re: Insert new pattern in newline after the nth occurrence of a line pattern - Bash i I think your main problem is that the sed 's/pattern/replacement/n' is meant to replace the nth occurrence within a single line. At vero eos et justo duo dolores et accusam et ea rebum. txt, but I would. If the element is created from an XML file the attribute will contain any text found after the element’s end tag and before the next tag. Useful tool for manipulating large data files in unix environments. txt G - Append hold space to pattern space. Add new line after. How do I insert a newline into the RHS of a substitution? Several versions of sed permit '\n' to be typed directly into the RHS, which is then converted to a newline on output: ssed, gsed302a+, gsed103 (with the -x switch), sed15+, sedmod, and UnixDOS sed. sed commands list (sed, a stream editor) Add a newline to the pattern space, then append the next line of input to the pattern space. According. Since its an “non-interactive” it can be used to. GitHub Gist: instantly share code, notes, and snippets. In the insert mode, user can insert text. sed '$ new text' myfile sed: 0602-404 Function $ new text cannot be parsed What did I get wrong?. Zero-or One-address commands:: = Print the current line number. My file formate for file. The modifier causes ^ and $ to match respectively (in addition to the normal behavior) the empty string after a newline, and the empty string before a newline. linux,shell,sed,grep,pattern-matching. Similar to inserting a line before a pattern, insert the line after the pattern as follows. sed 'n;G' man sed explains ~ as: first ~ step Match every step'th line starting with line first. Active 5 months ago. 101 -j DROP. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). SED is a very powerful utility which allows find and replace/insert text functionality. linux,shell,sed,grep,pattern-matching. How do I find and replace character codes ( control-codes or nonprintable characters ) such as ctrl+a using sed command under UNIX like operating systems? A. To add a new line with some content after every pattern match, use option 'a' , [[email protected] ~]$ sed '/danger/a "This is new line with text after match"' testfile. Welcome to the second part of our series, a part that will focus on sed, the GNU version. Stream processing tool. I prefer the first form because it's easier to add a new command by adding a new line and because the intent is clearer. While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. If the element is created from an XML file the attribute will contain any text found after the element’s end tag and before the next tag. Lines consist of text up to and including a newline character. So let's put these commands to good use:. Bourne scripting. I am trying to use sed in my bash script. Sed Insert Newline After Match. Hi, I have a file which contains many occurances of a string say "hellosunil". txt Linux Sysadmin Databases - Oracle, mySQL etc. The following example aims to remove the second word of a sentence: $ echo "Call me Ishmael" | sed 's/\([a-zA-Z]*\)//2' Call Ishmael. Insert last deleted text, in FRONT of curser SED DO NOT print lines that don't match the address-n~ SED Vi/Sed/Awk part c. txt is hello linux ; Nodes this is how can i append the text after pattern match using variable in sed. txt is hello linux ; Nodes this is how can i append the text after pattern match using variable in sed. The M modifier to regular-expression matching is a GNU sed extension which causes ^ and $ to match respectively (in addition to the normal behavior) the empty string after a newline, and the empty string before a newline. I have a bunch of files in a folder For all the files here is what I want to do: Insert ". So hold space holds nothing initially and if you append that to pattern space, you get a new line. Here's how I used a modified form of this answer to insert a newline between two matched patterns: sed '\(first match\)\(second match\)/\1\'$'\n''\2/g'. A sed script to insert text before and after a line. What if you don't have a line number instead you have a pattern name which you want to match and perform this action sed: add or append character at beginning or. The -i '' (or, in Linux, just -i) tells sed to use a zero-length extension for the backup. If there is no more input then sed exits without processing any more commands. q Immediately quit the sed script without processing any more input, except that if auto-print is not disabled the current pattern space will be printed. If you use sed you have to fork/exec an entirely new process (very expensive for something that the shell can do internally). Each SED command consists of an optional address or address range, followed by a one-character command name and any additional command-specific code. Q Immediately quit the sed script without processing any more input. Matching and manipulating multiple lines is fairly simple with sed. sed 'n;G' man sed explains ~ as: first ~ step Match every step'th line starting with line first. Zero-address ``commands'': label Label for b and t commands. Add the line "Cool gadgets and websites" after the 3rd line. The M modifier to regular-expression matching is a GNU sed extension which directs GNU sed to match the regular expression in multi-line mode. Whenever sed is executed on an input file or on the contents from stdin, sed reads the file line-by-line and after removing the trailing newline, places it in the "Pattern space", where the commands are executed on them after conditions (as in case of regex matching) are verified, and then printed on the stdout. txt You can also use sed for inserting before a line with.