Читать книгу Linux Bible - Christopher Negus - Страница 216

The stream editor (sed)

Оглавление

The sed command is a simple scriptable editor, so it can perform only simple edits, such as removing lines that have text matching a certain pattern, replacing one pattern of characters with another, and so on. To get a better idea of how sed scripts work, there's no substitute for the online documentation, but here are some examples of common uses.

You can use the sed command essentially to do what I did earlier with the grep example: search the /etc/passwd file for the word home. Here the sed command searches the entire /etc/passwd file, searches for the word home, and prints any line containing the word home:

 $ sed -n '/home/p' /etc/passwd chris:x:1000:1000:Chris Negus:/home/chris:/bin/bash joe:x:1001:1001:Joe Smith:/home/joe:/bin/bash

In this next example, sed searches the file somefile.txt and replaces every instance of the string Mac with Linux. Notice that the letter g is needed at the end of the substitution command to cause every occurrence of Mac on each line to be changed to Linux. (Otherwise, only the first instance of Mac on each line is changed.) The output is then sent to the fixed_file.txt file. The output from sed goes to stdout, so this command redirects the output to a file for safekeeping.

 $ sed 's/Mac/Linux/g' somefile.txt > fixed_file.txt

You can get the same result using a pipe:

 $ cat somefile.txt | sed 's/Mac/Linux/g' > fixed_file.txt

By searching for a pattern and replacing it with a null pattern, you delete the original pattern. This example searches the contents of the somefile.txt file and replaces extra blank spaces at the end of each line (s/ *$) with nothing (//). Results go to the fixed_file.txt file.

 $ cat somefile.txt | sed 's/ *$//' > fixed_file.txt

Linux Bible

Подняться наверх