Text Processing Three musketeers:
Grep,egrep,fgrep: Text filter Sed:stream Editor: Stream editor, line awk: Text formatting tool, Report Builder
Sed [option] ... ' script ' [input-file] script: Address delimitation Edit Command [option] -n: Does not output the contents of the mode space to the screen -e: multi-point editing -f /path/to/sed_script_file: -r:regexp-extend: Supports the use of extended regular expressions -i:--in-place: Directly edit the original document address delimitation: 1, empty address, processing of the full text     2, single address: #: Specify line / pattern/: Each line that is matched by this pattern 3, address range #,#: #,+#: #,/pat1/ /pat1/,/pat2/ 4, Stepping: 1~2: All odd lines 2~2: All even lines edit commands: d: Delete the contents of the pattern space p: Display content in the mode space a append:a \text: Append text "text" after line, support using \ nTo implement multiline append i insert:i \text: Insert text "text" before the line, Support for multi-line insert c \text with \ n: replace the matched line with the text specified here "text" w /path/to/somefile: Save the pattern space to match the line to the specified file r /path/to/somefile: Reads the contents of the specified file to the back of the line to which the current file is matched; =: Print line number for the line to which the pattern is matched !: Conditional Inversion Address delimitation! Edit Command sed '/^#/!d ' /etc/fstab sed '/^#/d ' /etc/fstab s///: Find and Replace, The delimiter can be specified by itself and is commonly used [email protected]@@,s## #等 Replacement Tags g: Global Substitution w /path/to/somefile: Saves the result of the replacement success to the specified file p: Display of rows that have been replaced successfully
Practice
1. Remove all white-space characters from the beginning of all lines in the/boot/grub/grub2.cfg file that begin with a blank character
2. Delete all white characters after # and # of all lines beginning with # in the/etc/fstab file
3. Output an absolute path to the SED command and take out its directory, which behaves like dirname
Analytical
[email protected] grub]# Cat grub.conf | Sed ' [Email protected]^[[:space:]]\[email protected]@g '
[Email protected] grub]# Cat/etc/fstab | Sed ' [Email protected]^#[[:space:]]*@@ '
Path name: [[email protected] grub]# echo "/var/log/messages/" | Sed ' [Email protected][^/]\+/\[email protected]@g '
Path name: [[email protected] grub]# echo "/va" | Sed-r ' [email protected] (. */) ([^/].*)/[email Protected]\[email protected] '
Base name: [[email protected] grub]# echo "/var/log/messages" | Sed-r ' [Email protected]*/([^/].*)/[email Protected]\[email protected] '
This article is from the "Homecoming" blog, make sure to keep this source http://sixijie123.blog.51cto.com/11880770/1885816
Text processing the-sed basic usage of the Three Musketeers