Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Adding, Changing, Inserting new lines

Sed has three commands used to add new lines to the output stream. Because an entire line is added, the new line is on a line by itself to emphasize this. There is no option, an entire line is used, and it must be on its own line. If you are familiar with many UNIX utilities, you would expect sed to use a similar convention: lines are continued by ending the previous line with a "\". The syntax to these commands is finicky, like the "r" and "w" commands.

Append a line with 'a'

The "a" command appends a line after the range or pattern. This example will add a line after every line with "WORD:"

#!/bin/sh

sed '

/WORD/ a\

Add this line after every line with WORD

'


Click here to get file: sed_add_line_after_word.sh

You could eliminate two lines in the shell script if you wish:

#!/bin/sh

sed '/WORD/ a\

Add this line after every line with WORD'


Click here to get file: sed_add_line_after_word1.sh

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. There must not be a space after the "\".

Insert a line with 'i'

You can insert a new line before the pattern with the "i" command:

#!/bin/sh

sed '

/WORD/ i\

Add this line before every line with WORD

'


Click here to get file: sed_add_line_before_word.sh

Change a line with 'c'

You can change the current line with a new line.

#!/bin/sh

sed '

/WORD/ c\

Replace the current line with the line

'


Click here to get file: sed_change_line.sh

A "d" command followed by a "a" command won't work, as I discussed earlier. The "d" command would terminate the current actions. You can combine all three actions using curly braces:

#!/bin/sh

sed '

/WORD/ {

i\

Add this line before

a\

Add this line after

c\

Change the line to this one

}'


Click here to get file: sed_insert_append_change.sh

Leading tabs and spaces in a sed script

Sed ignores leading tabs and spaces in all commands. However these white space characters may or may not be ignored if they start the text following a "a," "c" or "i" command. In SunOS, both "features" are available. The Berkeley (and Linux) style sed is in /usr/bin, and the AT&T version (System V) is in /usr/5bin/.

To elaborate, the /usr/bin/sed command retains white space, while the /usr/5bin/sed strips off leading spaces. If you want to keep leading spaces, and not care about which version of sed you are using, put a "\" as the first character of the line:

#!/bin/sh

sed '

a\

\ This line starts with a tab

'

Adding more than one line

All three commands will allow you to add more than one line. Just end each line with a "\:"

#!/bin/sh

sed '

/WORD/ a\

Add this line\

This line\

And this line

'

Adding lines and the pattern space

I have mentioned the pattern space before. Most commands operate on the pattern space, and subsequent commands may act on the results of the last modification. The three previous commands, like the read file command, add the new lines to the output stream, bypassing the pattern space.




Date: 2016-01-14; view: 1112


<== previous page | next page ==>
Operating in a pattern range except for the patterns | Multi-Line Patterns
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.008 sec.)