Two text editing artifacts in Unix- awk and sed . We have introduced awk before.
So today learn some sed.
sed is short for stream editor . Sed can complete very complex tasks through simple command operations on the command line, and is a very powerful and useful tool.
Before learning sed, we first create the following text file for follow-up exercises: example.txt
This is a test file. It is the last day of 2018. Hope all you success! HAPPY NEW YEAR!
Basic usage
Basic usage:
sed "s/regular expression (or specific character to be replaced)/target character/flag" operation file
(Hint: You can swipe left and right)
If you know sed, then your most familiar application is to use s for text replacement. For example, replace 2018 in example.txt with 2019.
$ sed "s/2018/2019/" example.txt
This is a test file.
It is the last day of 2019.
Hope all you success!
HAPPY NEW YEAR!
If you want to replace is in exmaple.txt with was:
$ sed "s/is/was/" example.txt
Thwas is a test file.
It was the last day of 2018.
Hope all you success!
HAPPY NEW YEAR!
It is very strange that the is in the second line is replaced with was, and the is in the first line is not replaced, but "this" is replaced with "thwas", this is because in the above command line, sed only replaces by default The first "is" in each line .
Use & to indicate the character to be replaced
For example, in example.txt, put all numbers in parentheses
$ sed "s/[0-9]/(&)/g" example.txt
This is a test file.
It is the last day of (2)(0)(1)(8).
Hope all you success!
HAPPY NEW YEAR!
Among them, [0-9] is only a regular expression, which means the numbers 0-9, and & means what you want to replace before. You don’t know which number it is, so you can use & instead. g means global , which means not only Only the first part of each line that meets the conditions is replaced, but all the characters that meet the conditions are replaced.
For another example, change the first letter of each word to a capital letter:
$ sed "s/ [a-z]/\U&/g" example.txt
This Is A Test File.
It Is The Last Day Of 2018.
Hope All You Success!
HAPPY NEW YEAR
Among them, \U means to change the matching items into uppercase letters (\L means to lowercase letters) , and & means to keep the pattern after transformation.
Use \1 and \2 to represent matches
\1 means the content in the first bracket in the regular expression.
For example, in example.txt, you want to add all the numbers in parentheses. We used the & symbol to complete the task before, and you can also use \1 to complete this task below.
$ sed "s/\([0-9]\)/(\1)/g" example.txt
This is a test file.
It is the last day of (2)(0)(1)(8).
Hope all you success!
HAPPY NEW YEAR!
Note that the regular expression must be enclosed in parentheses, and the parentheses must have the antonym "\".
For example, we need to delete everything after the first lowercase letter in each line.
$ sed "s/\([a-z]\).*/\1/" example.txt
Th
It
Ho
HAPPY NEW YEAR!
To be more complicated, swap the first letter of the first word in each line:
$ sed "s/\(.\)\(\S*\)\(.\) /\3\2\1 /" example.txt
shiT is a test file.
tI is the last day of 2018.
eopH all you success!
YAPPH NEW YEAR!
Where \S represents a non-space character .
Here is an important point to remember: the content enclosed in parentheses in the regular expression can be quoted in the target string in the form of \1, \2, \3... (up to 9).
FLAG area: 1 and 2 indicate the position
As mentioned earlier, $ sed "s/[0-9]/(&)/g" example.txt
the role of g is to tell sed to replace all, not just the first one that meets the conditions in each line.
So what if you want to replace only the second one that meets the condition? Then you can use a number in the flag area to indicate which one meets the condition. Such as
$ sed 's/[0-9]/(&)/2' example.txt
This is a test file.
It is the last day of 2(0)18.
Hope all you success!
HAPPY NEW YEAR!
The 2 here means to enclose the second number of each line in parentheses.
FLAG area: /p means output the modified line
The parameter -n of sed means no result is output
$ sed -n 's/[0-9]/(&)/2' example.txt
# 没有输出结果
If we add p to the flag area, we will be able to output the modified lines, and other lines will not be output. So -n and p are often used in combination
$ sed -n 's/[0-9]/(&)/2p' example.txt
It is the last day of 2(0)18.
FLAG area: /w output the result
Use the format " /w file name ", and output the result to the tt.txt file as follows:
$ sed 's/[0-9]/(&)/w tt.txt' example.txt
This is a test file.
It is the last day of (2)018.
Hope all you success!
HAPPY NEW YEAR!
$ cat tt.txt
It is the last day of (2)018.
Note that the output here is only the processed line output, the unprocessed line will not be output to tt.txt , so this is not the same as the output using redirection ">"! !
FLAG area: /I means ignore case
Such as
$ sed 's/t/HHHH/I' example.txt
HHHHhis is a test file.
IHHHH is the last day of 2018.
Hope all you success!
HAPPY NEW YEAR!
Both uppercase T and lowercase t have been replaced by HHHH.
****** A hundred reading is worse than a practice **********
===== THE END ====