In Linux, The SED command is used to replace the text. The command that matches the regular expression can be replaced flexibly with SED, which greatly reduces the workload. It can be said that SED is the main force of replacement in shell scripts and one of the most widely used commands in shell scripts. Sed is a line-based compiler, that is, it prints the row of matched content, and sed has its own mode space (memory), that is, sed does not change the content of the original file, however, sed-I can directly modify the original file (this function is used with caution and can easily cause losses)
Sed format:
Sed [Option]/pattern/command files
Sed [Option] ADDR, ADDR/command files
-N // avoid displaying unmatched rows
-I // directly modify the original file
Three basic regular expressions can be used with sed to match words.
\ <Or \ B // anchor of the beginning of the anchor [^] // reversed, a character other...
\> Or \ B // pin the end of the word
# Use sed in a simple example:
Sed 1, 3 P/proc/cpuinfo
This command prints the content of cpuinfo in the first three rows twice. If only the first three rows are displayed, the-n option is added. Here, 1 and 3 indicate the first to three rows. P indicates printing.
# Extend several sed commands:
P print/I insert before matching content
/D delete/A Insert after matching content
S replacement/G Global replacement
At the same time, sed can also use a regular expression:
For example:
Sed "/^ model/A \ # This is my CPU."/proc/cpuinfo
# Have you seen ^? This command inserts # This is my CPU after the model. Note that it is a line after the model line.
Next we will introduce the replacement Mode S/the content to be searched/the content to be replaced/command
For example:
Sed '1, $ S/Yes/'/proc/cpuinfo
# Replace yes from 1 to the last line in/proc/cpuinfo with yes.
#1, $ indicates matching the last line from the first line to the last line $ here, $-2, and so on.
# You can also use/G Global replacement, that is, replace all the matching items
Okay. Several exercises about sed:
Sed exercises
1. In the/etc/inittab file, change the number between the two colons in the line that begins with ID and has two colons and one number between the two colons to 3;
2. Change the first letter of all words starting with N in the/etc/passwd file to uppercase;
3. In the/proc/meminfo file, add a new line "# for grouping" after all rows starting with hugepages;
4. Delete all rows starting with # Or starting with a blank character or a # in the/etc/inittab file, change the number at the end of the row ending with a space followed by a number to 0;
Answer: 1. Sed s @ ^ ID: [0-9]: @ ID: 3: @ g/etc/inittab
# Here @ is also an alternative symbol. This technique is useful when the text to be replaced is a path.
2. Sed '1, $ S/\ BN/n/PG '/etc/passwd
3. Sed "/hugepages/A \ # for clustering"/proc/meminfo
4. sed-e '/^ [[: Space:] * #. */D'/etc/inittab-e "s/[[: Space:] [0-9] $/0/G"/etc/inittab
# The-e option is used here. The two sed commands are processed because the beginning of the text contains an empty character, so ^ [[: Space:] * # matches the first requirement, * represents 0 to any
--------------------------------------------------------------------------------
The TR command can easily replace a specified character with another character. For example, replace lowercase letters with uppercase and lowercase letters, and use sed with TR to process rows, you can easily and quickly replace characters in a row.
Example: Echo ABCD | tr 'AB' // replace AB with AB
TR has a commonly used option-D.-delete deletes the characters in Set 1 instead of the conversion.
Eco "banana" | tr-D 'A'
# A sed and TR script
Write a script:
1. Change the first and last letters of all files in the/var/directory to uppercase;
#!/bin/bash#: Title:lsvar #: Synopsis: #: Date:2011-07-26 23:35:27#: Version: 1.0#: Author: Dean #: Options:for FILE in `ls /var`; do FLITER=`echo "$FILE" | sed '1,$s/\([a-zA-Z]\).*/\1/g' | tr 'a-z' 'A-Z'` LLITER=`echo "$FILE" | sed '1,$s/.*\([a-zA-Z]\)/\1/g' | tr 'a-z' 'A-Z'` echo "$FILE" | sed "s/[a-zA-Z]\(.*\)[a-zA-Z]/$FLITER\1$LLITER/g"done