katasuka wrote:
...PS: also can someone give me working examples with explainations for how to use sed i think it is to find one line in a file and add 3 lines in place of it. despite all the research ive done on sed, i just cant seem to grasp it.
The best tutorial (especially for beginners) I've come across is Bruce Barnett's UNIX Grymoire tutorial,
Sed - An Introduction and Tutorial, which puts things into a nice, easy-to-learn perspective.....I highly recommend reading it.......
For starters, you will need to read up on line addressing (locating a particluar line to edit) combined with one of three sed commands (not to be confused with the 'sed' options), which are "insert" (i), "append" (a) and "change" (c)........A brief summary of the commands are:
- i: Insert a line - This command will insert text before the line specified in the line address. Also, this command will only accept a single address (line) to insert the text, as opposed to inserting text before an address range (sequence of multiple lines). This does not mean it will only work on one line and then quit, unless you're using line numbers for the line address. If using /regexp/ matching, it will insert the text before each line that matches the expression.
- a: Append a line - This command will insert text after the matching line, and follows the same rules as the Insert command of working with a single line only, which actually makes sense since specifying a range of lines is rather pointless for just inserting/appending text.
- c: Change a line - This command will replace a line, or multiple adjacent lines (range) with text. The best way to think of this command is to substitute the line(s) with text, similar to the commonly used "substitute" command (s///) but works with whole line(s) rather than snippets of text in a single line.
IMPORTANT: When adding multiple lines of text, each separate line must have a single backslash at the end of the line and must be the very last character of the line. This also includes the command itself if placing the replacement text on a separate line of it's own (ie.,
i\,
a\,
c\).........The backslash can be omitted with last line of the replacement text.......
Next is specifying which line(s) of text to work on using line addressing..........Addresses are of three basic formats:
- Line numbers - Specify a line simply by using it's line number.
- Last line ($) - Matches the last line of text before the end-of-file marker. Think of it as a special line number where you don't actually know how many lines are in the file.
- Pattern matching (/regexp/) - Uses regular expression matching to find lines containing the specified pattern. The pattern is enclosed with forward slashes (/pattern/), which is a typical format used by unix commands to indicate regular expression matching. NOTE: If the pattern contains any forward slashes, you will need to escape each occurrence of the forward slash with a backslash(\). However, sed provide a "shortcut" method which allows you to use any single character in place of the enclosing forward slashes, like you can with the substitute command (s@@@). To enable this feature, merely use a backslash immediately before the desired character, such as \@pattern@. This will allow you to use a forward slash in the pattern without the need to escape it with a backslash. Just be sure to NOT use a character found in the pattern itself.

To specify a range of adjacent lines, a comma (
,) is placed between the first address and last address in the range, and can be any mix of line numbers, last line or pattern matching......See the SED manpage for a quick summary on line addresses
So, if you wish to replace a single line of text with three different lines of text, you can state it like this in a shell script:
Code:
Example 1:
sed '24c\
Replacement line one.\
Replacement line two.\
Replacement line three.\
'
Example 2:
sed '$ c \
Replacement line one.\
Replacement line two.\
Replacement line three.\
'
Example 3:
sed '/pattern/ c \
Replacement line one.\
Replacement line two.\
Replacement line three.\
'
The first example will replace line number 24 with the three lines of text, the second example will replace the last line with the text, and the third example will replace any line containing the matching pattern with the lines of text.........The examples also show it makes no difference if spaces surrounding (before or after) the command are included.......Note the enclosing single quotes in each example.....
TIP: I like to place the final single quote on a line by itself to make it easier to add or remove lines of replacement text, and also include an ending backslash with the last line for consistency.....
You may also write the command on a single line, using the newline escape sequence (\n), like so:
Code:
Example 1:
sed '24cReplacement line one.\nReplacement line two.\nReplacement line three.'
Example 2:
sed '$ c Replacement line one.\nReplacement line two.\nReplacement line three.'
Example 3:
sed '/pattern/ c Replacement line one.\nReplacement line two.\nReplacement line three.'
While the space is optional surrounding the 'c' command, placing a single space following the command makes for easier reading, and the ending backslash is not used.........
HTH
---thegeekster