Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Filenames on the command line

You can specify files on the command line if you wish. If there is more than one argument to sed that does not start with an option, it must be a filename. This next example will count the number of lines in three files that don't begin with a "#:"

sed 's/^#.*//' f1 f2 f3 | grep -v '^$' | wc -l

Let's break this down into pieces. The sed substitute command changes every line that starts with a "#" into a blank line. Grep was used to filter out (delete) empty lines. Wc counts the number of lines left. Sed has more commands that make grep unnecessary. And grep -c can replace wc -l. I'll discuss how you can duplicate some of grep's functionality later.

Of course you could write the last example using the "-e" option:

sed -e 's/^#.*//' f1 f2 f3 | grep -v '^$' | wc -l

There are two other options to sed.

sed -n: no printing

The "-n" option will not print anything unless an explicit request to print is found. I mentioned the "/p" flag to the substitute command as one way to turn printing back on. Let me clarify this. The command

sed 's/PATTERN/&/p' file

acts like the cat program if PATTERN is not in the file: e.g. nothing is changed. If PATTERN is in the file, then each line that has this is printed twice. Add the "-n" option and the example acts like grep:

sed -n 's/PATTERN/&/p' file

Nothing is printed, except those lines with PATTERN included.

The long argument of the -n command is either

sed --quiet 's/PATTERN/&/p' file

or

sed --silent 's/PATTERN/&/p' file

Using 'sed /pattern/'

Sed has the ability to specify which lines are to be examined and/or modified, by specifying addresses before the command. I will just describe the simplest version for now - the /PATTERN/ address. When used, only lines that match the pattern are given the command after the address. Briefly, when used with the /p flag, matching lines are printed twice:

sed '/PATTERN/p' file

And of course PATTERN is any regular expression.

Using 'sed -n /pattern/p' to duplicate the function of grep

If you want to duplicate the functionality of grep, combine the -n (noprint) option with the /p print flag:

sed -n '/PATTERN/p' file

Sed -f scriptname

If you have a large number of sed commands, you can put them into a file and use

sed -f sedscript <old >new

where sedscript could look like this:

# sed comment - This script changes lower case vowels to upper case

s/a/A/g

s/e/E/g

s/i/I/g

s/o/O/g

s/u/U/g

When there are several commands in one file, each command must be on a separate line.

The long argument version is

sed --file=sedscript <old >new

Also see here

Sed in shell scripts

If you have many commands and they won't fit neatly on one line, you can break up the line using a backslash:

sed -e 's/a/A/g' \

-e 's/e/E/g' \

-e 's/i/I/g' \

-e 's/o/O/g' \

-e 's/u/U/g' <old >new

Quoting multiple sed lines in the C shell



You can have a large, multi-line sed script in the C shell, but you must tell the C shell that the quote is continued across several lines. This is done by placing a backslash at the end of each line:

#!/bin/csh -f

sed 's/a/A/g \

s/e/E/g \

s/i/I/g \

s/o/O/g \

s/u/U/g' <old >new


Date: 2016-01-14; view: 813


<== previous page | next page ==>
The slash as a delimiter | Using sed in a shell here-is document
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.007 sec.)