How Linux displays a few lines of a file (in the middle of a few lines)
"One" starts at line No. 3000 and displays 1000 rows. The 3000~3999 line is displayed
Cat FileName | Tail-n +3000 | Head-n 1000
"Two" displays 1000 rows to 3000 rows
Cat FileName | Head-n 3000 | Tail-n +1000
* Note the order of the two methods
Decomposition:
Tail-n 1000: Show last 1000 rows
Tail-n + 1000: Starting from 1000 lines, showing 1000 rows later
Head-n 1000: Show front 1000 lines
"Three" with sed command
Sed-n ' 5,10p ' filename so you can only view lines 5th through 10th of the file.
Number of Linux statistics file lines
Syntax: WC [options] File ...
Description: This command counts the number of bytes, words, and lines in a given file. If the file name is not given, it is read from the standard input. The WC also gives the president count of all specified files. The word is the largest string separated by the space character area.
The command options have the following meanings:
-C Count bytes.
-L COUNT the number of rows.
-W counts the number of words.
These options can be used in combination.
The order and number of output columns are not affected by the order and number of options.
Always appear in the following order and at most one column per item.
Number of rows, words, bytes, filenames
If there is no file name in the command line, the file name does not appear in the output.
For example:
$ WC-LCW file1 file2
4 file1
7 File2
Total
Example Analysis:
1. Count the number of JS files in the demo directory:
Find demo/-name "*.js" |wc-l
2. Statistics of all JS file code lines in the demo directory:
Find demo/-name "*.js" |xargs cat|wc-l or Wc-l ' find./-name ' *.js ' |tail-n1
3. Statistics of all JS file code lines in the demo directory, Filtered Blank lines:
Find/demo-name "*.js" |xargs cat|grep-v ^$|wc-l
1. Introduction
SED is a non-interactive editor. It does not modify the file unless you use Shell redirection to save the results. By default, all output lines are printed to the screen.
The SED editor processes the file (or input) row by line and sends the results to the screen. The process is as follows: first SED saves the row currently being processed in a temporary buffer (also known as pattern space), and then processes the rows in the temporary buffers and sends the line to the screen when it is complete. Sed deletes a row from the temporary buffer after each processing, and then reads the next line in, processing, and displaying it. After the last line of the input file has been processed, sed finishes running. SED has a temporary buffer in each row and edits the copy, so the original file is not modified.
2. Addressing
Addressing is used to decide which rows to edit. The address can be in the form of a number, a regular expression, or a combination of both. If no address is specified, SED processes all lines of the input file.
The address is a number that represents the line number; a "$" symbol, which indicates the last row. For example:
Sed-n ' 3p ' datafile
Print only the third line
Displays only the file contents of the specified line range, for example:
# View only lines 100th through No. 200 of the file
Sed-n ' 100,200p ' Mysql_slow_query.log
Addresses are separated by commas, the address that needs to be processed is the range between the two lines, including both. Ranges can be represented by numbers, regular expressions, or a combination of both. For example:
Sed ' 2,5d ' datafile
#删除第二到第五行
Sed '/my/,/you/d ' datafile
Rows #删除包含 "My" to rows that contain "you"
Sed '/my/,10d ' datafile
#删除包含 the contents of line to line tenth of "My"
3. Commands and Options
The sed command tells SED how to handle each input line specified by the address, and all input lines are processed if no address is specified.
3.1 sed command
Command function
A\
Adds one or more rows after the current line. Multiple rows, except for the last row, need "\" to continue at the end of each line
C\ replaces the text in the current line with the new text after this symbol. Multiple rows, except for the last row, need "\" to continue at the end of each line
I\ inserts text before the current line. Multiple rows, except for the last row, need "\" to continue at the end of each line
D Delete Row
H Copy the contents of the schema space to the staging buffer
H Append the contents of the schema space to the staging buffer
G Copy the contents of the staging buffer to the schema space, overwriting the original content
G Append the contents of the staging buffer to the schema space, appended to the original content
L List nonprinting characters
P Print Line
N reads into the next input line and starts processing it from the next command instead of the first command
Q End or exit sed
R reads the input line from the file
! Apply commands to all rows except the selected row
S replaces another with a string
G Global substitution within a row
W writes the selected line to the file
X-Swap the contents of the staging buffer with the pattern space
Y replaces the character with another character (you cannot use the Y command with regular expressions)
3.2 sed options
Options feature
-e multiple edits, which are used when multiple SED commands are applied to an input line
-N cancels the default output
-f Specifies the file name of the SED script
4. Exit status
SED does not like grep, regardless of whether the specified pattern is found, its exit status is 0. The exit status of SED is not 0 only if there is a syntax error in the command.
5. Regular expression meta-characters
Like grep, SED also supports special meta-characters for pattern lookups and substitutions. The difference is that the regular expression used by SED is the pattern enclosed by the slash line "/".
If you want to change the regular expression delimiter "/" to another character, such as O, simply add a backslash before the character, followed by the regular expression after the character, followed by the character. Example: Sed-n ' \o^myop ' datafile
Meta-character Feature example
^ Line Locator/^my/matches all lines starting with my
$ line Tail Locator/my$/matches all lines ending with my
. Matches a single character, except for newline characters/M. y/matches the line that contains the letter m, followed by two arbitrary characters, and then the letter Y
* Matches 0 or more leading characters/my*/matches a line containing the letter m followed by 0 or more Y-letters
[] matches any character within the specified character group/[mm]y/matches the row containing my or my
[^] matches any character that is not within the specified character group/[^mm]y/matches the line containing Y, but before y the character is not m or m
\(.. \) Save the pattern that matches the character 1,20s/\ (you\) self/\1r/tag metacharacters, save it as label 1, and then use \1 to refer to it. You can define up to 9 labels, numbering from the left, and the first one on the left. In this example, rows 1th through 20th are processed, you are saved as label 1, and if youself is found, it is replaced with your.
& Save the Find string to reference the s/my/**&**/symbol in the replacement string & represents the Find string. My will be replaced with **my**
\< Word first locator/\<my/matches lines containing words that begin with my
\> ending locator/my\>/matches lines that contain words ending with my
X\{m\} consecutive M-x/9\{5\}/matches a row containing 5 consecutive 9
X\{m,\} at least M x/9\{5,\}/matches a row containing at least 5 consecutive 9
x\{m,n\} at least M, but no more than n x/9\{5,7\}/matches rows containing 5 to 7 consecutive 9
6. Example
6.1 P Command
The command p is used to display the contents of the pattern space. By default, sed prints the input lines on the screen, and option-n is used to cancel the default print operation. When the Select-N and command p appear simultaneously, sed prints the selected content.
Sed '/my/p ' datafile
#默认情况下, sed prints all input lines on the standard output. If a row matches the pattern, the my,p command will print the line again.
Sed-n '/my/p ' datafile
#选项-N Cancels the default print for SED, and the p command prints the line of the matching mode my.
6.2 d Command
Command d is used to delete input rows. Sed first copies the input line from the file into the pattern space, then executes the SED command on the line, and finally displays the contents of the pattern space on the screen. If command d is emitted, the input lines in the current mode space are deleted and not displayed.
Sed ' $d ' datafile
#删除最后一行, the rest is displayed.
Sed '/my/d ' datafile
#删除包含my的行, the rest is displayed.
6.3 S command
Sed ' s/^my/you/g ' datafile
#命令末端的g表示在行内进行全局替换, that is, if a row appears with multiple my, all my my is replaced with you.
Sed-n ' 1,20S/MY$/YOU/GP ' datafile
#取消默认输出, Process 1 to 20 lines matching the line ending with my, replace all of my in-line I with you and print to the screen.
Sed ' s#my#your#g ' datafile
#紧跟在s命令后的字符就是查找串和替换串之间的分隔符. Separating defaults is considered a forward slash, but can be changed. No matter what character (except newline, backslash), as soon as the S command is followed, a new string delimiter is formed.
6.4 E option
-E is an edit command that is used in cases where SED performs multiple editing tasks. All edit actions are applied to the line in the pattern buffer before the next line begins editing.
Sed-e ' 1,10d '-e ' s/my/your/g ' datafile
#选项-E is used for multiple edits. The first edit deletes the 第1-3 line. The second editor replaces all occurrences of my with your. Because these two edits are performed line by row (that is, both commands are executed on the current line in the pattern space), the order in which the commands are edited affects the result.
6.5 r Command
The r command is a read command. SED uses this command to add the contents of a text file to a specific location in the current file.
Sed '/my/r introduce.txt ' datafile
#如果在文件datafile的某一行匹配到模式My, the contents of the file Introduce.txt are read after the line. If there is more than one row of my rows, the contents of the Introduce.txt file are read after the lines of my are present.
6.6 w Command
Sed-n '/hrwang/w me.txt ' datafile
6.7 a\ Command
The A\ command is an append command, appended with the addition of new text to the current line in the file (that is, the line in the read-in mode buffer). The appended line of text is placed below the SED command. If more than one row is to be appended, each row must end with a backslash, except for the last line. The last line ends with a quotation mark and a file name.
Sed '/^hrwang/a\
>hrwang and Mjfan are husband\
>and Wife ' DataFile
#如果在datafile文件中发现匹配以hrwang开头的行, append Hrwang and Mjfan are husband and wife below the line
6.8 i\ Command
The I\ command inserts a new text in front of the current line.
6.9 c\ Command
SED uses this command to modify an existing text to a new text.
6.10 N Command
SED uses this command to get the next line of input files and read them into the pattern buffer, and any sed command will be applied to the next line immediately following the matching line.
Sed '/hrwang/{n;s/my/your/;} ' datafile
Note: If you need to use multiple commands, or if you need to nest addresses in an address range, you must enclose the command in curly braces, write only one command per line, or use semicolons to split multiple commands in the same row.
6.11 y command
This command is similar to the TR command in Unix/linux, where characters are converted from left to right in a one-to-one fashion. For example, y/abc/abc/will convert all lowercase a to a, lowercase B to B, and lowercase C to c.
Sed ' 1,20y/hrwang12/hrwang^$/' datafile
#将1到20行内, convert all lowercase hrwang to uppercase, convert 1 to ^, convert 2 to $.
#正则表达式元字符对y命令不起作用. As with the S command delimiter, the slash can be replaced with other characters.
6.12 q command
The Q command will cause the SED program to exit without further processing.
Sed '/hrwang/{s/hrwang/hrwang/;q;} ' datafile
6.13 h command and G command
#cat datafile
My name is Hrwang.
Your name is Mjfan.
Hrwang is Mjfan ' s husband.
Mjfan is Hrwang ' s wife.
Sed-e '/hrwang/h '-e ' $G ' datafile
Sed-e '/hrwang/h '-e ' $G ' datafile
#通过上面两条命令, you will find that H clears the contents of the original staging buffer, saving only the schema space that was saved in the last time the H was executed. The h command appends the rows of each match Hrwnag to the staging buffer.
Sed-e '/hrwang/h '-e ' $g ' datafile
Sed-e '/hrwang/h '-e ' $G ' datafile
#通过上面两条命令, you'll find that G replaces the contents of the current row in the schema space with the content of the staging buffer, replacing the last row. The G command appends the contents of the staging buffer to the current line of the schema space. This is appended to the end.
How Linux uses commands to view a few lines of a file (middle lines or last few lines)