Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






An alternate way of adding comments

There is one way to add comments in a sed script if you don't have a version that supports it. Use the "a" command with the line number of zero:

#!/bin/sh

sed '

/begin/ {

0i\

This is a comment\

It can cover several lines\

It will work with any version of sed

}'


Click here to get file: sed_add_comments.sh

The poorly documented ;

There is one more sed command that isn't well documented. It is the ";" command. This can be used to combined several sed commands on one line. Here is the grep4 script I described earlier, but without the comments or error checking and with semicolons between commands:

#!/bin/sh
sed -n '
'/$1/' !{;H;x;s/^.*\n\(.*\n.*\)$/\1/;x;}
'/$1/' {;H;n;H;x;p;a\
---
}'


Click here to get file: grep4a.sh

Yessireebob! Definitely character building. I think I have made my point. As far as I am concerned, the only time the semicolon is useful is when you want to type the sed script on the command line. If you are going to place it in a script, format it so it is readable. I have mentioned earlier that many versions of sed do not support comments except on the first line. You may want to write your scripts with comments in them, and install them in "binary" form without comments. This should not be difficult. After all, you have become a sed guru by now. I won't even tell you how to write a script to strip out comments. That would be insulting your intelligence. Also - some operating systems do NOT let you use semicolons. So if you see a script with semicolons, and it does not work on a non-Linux system, replace the semicolon with a new line character. (As long as you are not using csh/tcsh, but that's another topic.

Passing regular expressions as arguments

In the earlier scripts, I mentioned that you would have problems if you passed an argument to the script that had a slash in it. In fact, regular expression might cause you problems. A script like the following is asking to be broken some day:

#!/bin/sh

sed 's/'"$1"'//g'

If the argument contains any of these characters in it, you may get a broken script: "/\.*[]^$" For instance, if someone types a "/" then the substitute command will see four delimiters instead of three. You will also get syntax errors if you provide a "]" without a "]". One solution is to have the user put a backslash before any of these characters when they pass it as an argument. However, the user has to know which characters are special.
Another solution is to add a backslash before each of those characters in the script

#!/bin/sh

arg=`echo "$1" | sed 's:[]\[\^\$\.\*\/]:\\\\&:g'`

sed 's/'"$arg"'//g'


Click here to get file: sed_with_regular_expressions1.sh
If you were searching for the pattern "^../," the script would convert this into "\^\.\.\/" before passing it to sed.

Command Summary

As I promised earlier, here is a table that summarizes the different commands. The second column specifies if the command can have a range or pair of addresses or a single address or pattern. The next four columns specifies which of the four buffers or streams are modified by the command. Some commands only affect the output stream, others only affect the hold buffer. If you remember that the pattern space is output (unless a "-n" was given to sed), this table should help you keep track of the various commands.



Command Address or Range Modification to Input Stream Modification to Output Stream Modification to Pattern Space Modification to Hold Buffer
= - - Y - -
a Address - Y - -
b Range - - - -
c Range - Y - -
d Range Y - Y -
D Range Y - Y -
g Range - - Y -
G Range - - Y -
h Range - - - Y
H Range - - - Y
i Address - Y - -
l Address - Y - -
n Range Y * - -
N Range Y - Y -
p Range - Y - -
P Range - Y - -
q Address - - - -
r Address - Y - -
s Range - - Y -
t Range - - - -
w Range - Y - -
x Range - - Y Y
y Range - - Y -

The "n" command may or may not generate output, depending on the "-n" option. The "r" command can only have one address, despite the documentation.

Check out my new Sed Reference Chart

In Conclusion

This concludes my tutorial on sed. It is possible to find shorter forms of some of my scripts. However, I chose these examples to illustrate some basic constructs. I wanted clarity, not obscurity. I hope you enjoyed it.

More References

This concludes my tutorial on sed. Other of my UNIX shell tutorials can be found here. Other shell tutorials can be found at

  • Heiner's SHELLdorado
  • Chris F. A. Johnson's UNIX Shell Page
  • The Wikipedia Entry on SED
  • SED one-liners
  • And don't forget The SED FAQ


This document was originally converted from NROFF to TEXT to HTML.
Please forgive errors in the translation.
If you are confused, grab the actual script if possible. No translations occurred in the scripts.


Date: 2016-01-14; view: 841


<== previous page | next page ==>
Keeping more than one line in the hold buffer | ADJUSTMENTS RELATED TO C-V, V-C LINKING
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.006 sec.)