Sed is a non-interactive stream editor that allows you to dynamically edit files.
Non-interactive mode: unlike traditional text editors, sed does not directly interact with users. The object processed by SED is the stream of files ).
The SED working mode is to compare each data row. If the style is correct, the specified operation is executed.
Syntax format:
Sed 'style command 'file
If a line in the file meets the "style command", run the specified sed command, such as Delete (d), replace (s), and output (P ).
Here, the "style command" is enclosed by a pair of // for searching.
For example:
1,/, indicating 1 to 6 rows
2./AAA/,/DDD/, indicates the line containing the AAA character to the line containing the DDD character
Note: sed does not change the file content. Sed reads the file content and outputs the result to the standard output after being edited by the stream. Therefore, if you want to save the SED processing result, you need to use the redirection.
Example:
1. delete data rows in a certain range
sed '1,4d' filepath
2. delete a row containing the specified "style"
sed '/styleText/d' filepath
3. Delete blank rows
sed '/^$/d' filepath
4. Delete rows with unspecified "styles"
sed '/styleText/!d' filepath
5. Output rows matching the "style"
sed -n '/styleText/p' filepath
The command P will display the current data, but because SED will also display non-conforming data rows by default, you need to use the option "-n ".
-N: suppress the display of non-conforming data rows.
6. Delete the three characters starting with each line
sed 's/^...//' filepath
7. Get the matching string
sed -n 's/\(styleText\)/\1string/p' filepath
() Used to store matching characters
\ 1 get the matched characters
8. Find the matched data row and then replace the command.
sed -n '/AAA/s/123/456/p' filepath
Find the row containing AAA, replace 123 with 456
9. Replace the command on the specified line
sed -n '2,4s/123/456/p' filepath
In lines 2nd to 4, replace 123 with 456
Awk to be continued