In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "introduction to the use of sed commands in Linux system". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the introduction to the use of sed commands in the Linux system.
Sed is a linux lightweight stream editor for row operations, mainly for data selection, replacement, deletion, addition, and so on. Read one line at a time from the input file
Apply the commands in the list to each row in order, and write the edited lines to standard output.
Authority limits: all users
Location: / bin/sed
Usage: sed [- nefr] [action] file
Parameters:
-n
When-n is not added, both the data processed by sed and those not processed by sed will be output, plus-n will only output the rows of sed operations.
Note: use it in conjunction with action p, not with other actions
-e
Sed action editing directly on the command line mode
For example: sed-e's AAA file b file, that is, multiple actions are operated simultaneously, pay attention to the semicolon in the middle.
-f
Write the sed action directly in a file, and-f file can perform the sed action in the filename
-r
Sed's actions support extended regular expressions (the default supports basic regular expressions)
-I
Directly modify the contents of the read file
Note: when multiple option parameters are used together, for example: sed-I-r. You can't do this: sed-ir.
Action description: [N1 [, N2]] function
It may not exist, it generally represents the number of rows of the selected action, and if the action is replaced between 5 and 15 lines, then "10th 20c"
An added, the next line of the current line
Sed'2a hello world' file output inserts hello world after line 2
Sed'2a hello world\
My name is csdn' file
The output inserts two lines below 2 lines, hello world and my name is scdn.
Pay special attention to the "\" after hello world
C replacement, which is the whole line replacement
Sed '3Jing 6c hello csdn' file
The output replaces only lines 3 to 6 with hello csdn (lines 3 to 6 have only one hello csdn left, not every line of lines 3 to 6 is hello csdn)
D Delete
Sed '3Jing 6d' file output content after deleting lines 3 to 6 of file
I insert, the previous line of the current line
Similar to an action, refer to the usage of an action
P print
Sed-n'2 file 5p 'print lines 2 to 5 of file
S replacement
Sed's file oldString file oldString and newString support basic regular expressions and extended regular expressions
Cat-n nginx | sed '1Magne99s / [0-9]\ {1,\}\. [0-9]\ {1,\}\. [0-9]\ {1,\}\. Replace the first field IP address of lines 1 to 99 of the nginx log with an empty IP address.
The result is:
91-[15/Dec/2014:23:00:10 + 0800], [15/Dec/2014:23:00:10 + 0800], GET / identified?key=this-is-TMP-apikey&digest=914EBB2FD551844CBE15513C0126E6EE&digest-algorithm=ed2k&hash=9D37BB2988F26973371FAB028FEEEB93D5542300& HTTP/1.1200 65Grader XMP Windows Client 4.9memoir 2ktrain / | file | ki141213.wmv | 1119747351 | 914EBB2FD551844CBE15513C0126E6EE | hmae 00E04C377725NG4EMagi Ki141213.wmvMagol 111974735192-[15/Dec/2014:23:00:10 + 0800], GET / identified? HTTP/1.1401 112 GET-93-[15/Dec/2014:23:00:10 + 0800], GET / identified? HTTP/1.1401 112 GET-94-[15/Dec/2014:23:00:10 + 0800], GET / identified? HTTP/1.1401 112 15/Dec/2014:23:00:10-95-- [15/Dec/2014:23:00:10 + 0800], -, 400 0 15/Dec/2014:23:00:10-- 96-- [15/Dec/2014:23:00:10 + 0800], GET / identified?key=this-is-TMP-apikey&digest=76BB7C854168DCD1BF15FCB92E4114FFD4CFE7DE&digest-algorithm=magnet&hash=28003498535& HTTP/1.1200 65 GET under Windows Client 7.9 Magnet:?xt=urn:btih:76BB7C854168DCD1BF15FCB92E4114FFD4CFE7DE,EC55F9E92A5212LQ,ABP-209R,1067178826 97-[15/Dec/2014:23:00:10 + 0800], GET / identified? HTTP/1.1401 112 HTTP/1.1401-98-[15/Dec/2014:23:00:10 + 0800], GET / identified? HTTP/1.1401 112 15/Dec/2014:23:00:10-99-- [15/Dec/2014:23:00:10 + 0800], -, 400 0-100 121.12.57.154-- [15/Dec/2014:23:00:10 + 0800], GET / identified? HTTP/1.1401 112J-
Other uses of action 1:sed'/ mode / action 'file
You can specify a mode of marching action as follows (the file name here is a, notice the output of cat a):
1. Precede the lines starting with p with [TAB]
$cat apa:11:asa:32:capp:5:bstort:1:dpear:4:aahello:3:f$ sed'/ ^ p/s/ ^ /\ t cat apa:11:asa:32:capp:5:bstort:1:dpear:4:aahello:3:f$ sed 'apa:11:asa:32:capp:5:bstort:1:dpear:4:aahello:3:f
2. Delete the line that starts with a (then the following output, the line that starts with an is gone)
$sed'/ ^ a Compact d'apa:11:asa:32:cstort:1:dpear:4:aahello:3:f
3. Reverse matching (in file a, the output only retains the lines that begin with a)
$sed'/ ^ a _ aapp:5:b
4. Equivalent to grep-v (a line in the output file that does not begin with the p letter)
$grep-v'^ h 'apa:11:asa:32:capp:5:bstort:1:dpear:4:aa$ sed-n' / ^ h _ apa:11:asa:32:capp:5:bstort:1:dpear:4:aa
Other uses of action 2:
You can also use pairs of regular expressions or a combination of line numbers and regular expressions to select a range of lines, as follows:
1. Sed '1re3D' file deletes 1 to 3 lines
2. Sed-n '1p3p' file print 1 to 3 lines
3. Sed'1, / ^ $/ d 'file delete range: the first line to the first blank line (pay attention to the format)
4. Sed'/ ^ $/, / ^ app/d' file delete the range: the first blank line to the line starting with app
5. Sed'$d' file deletes the last line
Note: the reverse matching line is preceded by an exclamation point (! d! s! C, etc.)
If the previous command adds-I directly, modify the file directly
At this point, I believe you have a deeper understanding of the "introduction to the use of sed commands in the Linux system". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.