Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ Linux, grep, sed usage     - TCP network communication Java Programming (Programming)

- Adjustment expand VMDK format VirtualBox disk space (Linux)

- How apt-get limited use IPv4 or IPv6 protocol to download (Linux)

- Unable to start the network after restart clone a virtual machine (Linux)

- Installation on the way to the root source Ubuntu Server 14.04 LTS version Odoo8.0 (Server)

- Ubuntu 14.10 splash screen brightness settings (Linux)

- Use Python automatically cleared Android Engineering excess resources (Programming)

- AngularJS notes --- Data Binding (Programming)

- Use GLOBK batch command network adjustment (Linux)

- Security enhancements in Ubuntu ssh service (Linux)

- Linux serial port driver test (Linux)

- Ubuntu under VirtualBox virtual machine serial port settings (Linux)

- ASM Management - How to Rename diskgroup (Database)

- Docker ecosystem security is gradually maturing (Server)

- MySQL and MariaDB traditional master-slave cluster configuration (Database)

- Learning and Practice (Linux)

- Linux System Getting Started Learning: The Linux log (Linux)

- Verify the character set on MyCAT (Database)

- 11.2.04 Oracle RAC directory crfclust.bdb file is too large, Bug 20186278 (Database)

- Ubuntu achieve initialization iptables (Linux)

 
         
  Linux, grep, sed usage
     
  Add Date : 2017-04-13      
         
         
         
  Linux in terms of some of the commonly used commands.

1. grep command

grep is used to filter the content they need, the following syntax

grep Mayuyu --color

Filters out all the rows contain Mayuyu, grep is also support for regular expressions.

2. sed command

Using ordinary vim editor has two main problems when editing files

(1) When a file is relatively large, the need to open the files take up more memory.

(2) When editing a document must be interactive, it is difficult to call, such as vim.

sed stream editor belonging to the so-called stream editor is when editing a file, you can not read the entire file into memory, you can read line by line, after the operation is completed to read the next line, which uses less memory.

Usually most of the command string is replaced with, for example, in a file inside, you need to replace a string with another string, the following command

It said it would Hello.cpp file var replaced ans, need to replace the characters with \ < and \> enclosed.

Displays a string in the first few lines of the original file, use the following command

Int represents the beginning of the line that is the source file line 7, use the "=" display line.

sed command Detailed

1. Introduction
sed 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.
sed editor to process the file line by line (or input), and sends the results to the screen. Specific process is as follows: First sed the line currently being processed is stored in a temporary buffer zone (also called the pattern space), and then process the temporary buffer row, after the completion of the line to send to the screen. sed finished working on a line will be deleted from the temporary buffer, and then the next line is read, processed and displayed. After processing the last line of the input file, sed is ended run. Each row sed to exist temporary buffer, this copy editor, so it will not modify the original file.
 
 
2. addressing
Addressing is used to determine which rows to edit. Form of address can be a number, regular expression, or a combination of both. If the address is not specified, sed will process all the input file lines.
 
Address is a number that indicates the line numbers; the "$" symbol, it indicates the last row. E.g:
 
 
sed -n '3p' datafile
Print only the third row
 Display only the contents of the file specified line range, for example:

# See only the file line 100 to line 184
sed -n '100,200p' mysql_slow_query.log
 

Address is a comma-separated, then the address is the need to deal with the range between the two lines (including two lines included). You can use a digital scope, regular expressions, or combinations of both representation. E.g:

sed '2,5d' datafile
# Delete the second to fifth row
sed '/ My /, / You / d' datafile
Row row row between # Delete "My" to contain "You" of
sed '/ My /, 10d' datafile
Text Line # Delete "My" to the tenth row
3. Command and Option

sed command tells sed how to handle each input line designated by the address, if the address is not specified the process all the input lines.

3.1 sed command

 Command Function
 a \
 Add one or more lines after the current line. Multiple lines except the last line end of each line required "\" continuation

 c \ to replace the current line of text with the new text of this symbol. Multiple lines except the last line end of each line required "\" continuation
 i \ insert text before the current line. Multiple lines except the last line end of each line required "\" continuation
 d Delete Row
 h Copy the pattern space to the contents of the temporary storage buffer
 H to the contents of the pattern space is added to the staging buffer
 g Copy the contents of the staging buffer to the pattern space, overwriting the old contents
 G the contents of the staging buffer appended to the pattern space, appended to the original content later
 l lists the non-printing characters
 p print lines
 n reads the next input line, and from the next command instead of the first command begins its processing
 q end or exit sed
 r read input line from the file
 ! On all lines other than the selected command application
 s replace a string with another
 G Replace globally within the line
 
 w to write files to the selected row
 x exchange scratch buffer with the contents of the pattern space
 y The character is replaced by another character (you can not use regular expressions y command)
 

3.2 sed options

 Option Function
 -e Make multiple editing, that is used to apply a plurality of input lines sed command
 -n cancel the default output
 -f specifies the sed script file name
 
4. Exit Status
sed grep not to the same, regardless of whether find the specified mode, its exit status is 0. Only when the command syntax error, the exit status of sed was not 0.
 
 
 
5. The regular expression metacharacters
 Like with grep, sed also supports special metacharacters to find patterns, replace. The difference is that a regular expression sed use is enclosed in wire mode slash "/" between.
If you want a regular expression delimiter "/" is replaced by another character, such as o, long before this character a backslash, after the character to keep up with regular expressions, you can talk to this character. For example: sed -n '\ o ^ Myop' datafile
 
 Metacharacters function example
 ^ Beginning of a line locator / ^ my / my matches all names beginning with line
 $ End of line locator / my $ / matches all my end of the line
 . Matches any single character except a newline /m..y/ match contain letters m, followed by any two characters talk to the line of the letter y
 * Match zero or more of the preceding character / my * / matches contain letters m, followed by zero or more rows of letters y
 [] Matches any single character within the specified set of characters / [Mm] row y / or my My match contains the
 [^] Matches any character not in the specified character within the group / [^ Mm] y / matches contain y, but y before that character is not M or m line
 \ (.. \) To save the matching character 1,20s / \ (you \) self / \ 1r / mode meta tags between characters, and save it as a label, then you can use \ 1 to refer to it. Can define up to nine labels, numbered starting from the left, the far left is the first one. In this example, the first line 20 1 to be processed, you are saved as a label, if you find youself, then replace your.
 Find & Save string to reference the s / my in the replacement / ** & ** / symbol & Find your string. my will be replaced ** my **
 \ < Word first locator / \ < my / my matches containing the word beginning of the line
 \> Suffix Locator / my \> / containing matching words ending with my line
 x \ {m \} m consecutive x / 9 \ {5 \} / match consists of five consecutive rows 9
 x \ {m, \} at least m x / 9 \ {5, \} / matches comprises at least 5 consecutive lines 9
 x \ {m, n \} at least m but not more than n x / 9 \ {5,7 \} / matches comprising a continuous row 5-7 9

6. Examples
 
6.1 p command
P command displays the contents of the pattern space. By default, sed the input line printed on the screen, the default option -n to cancel the print operation. When the -n option and command p simultaneously, sed can print the selected content.
 
sed '/ my / p' datafile
# By default, sed to print all input lines to standard output. If a line matching pattern my, p command will print the additional line again.

sed -n '/ my / p' datafile
# Sed -n option to cancel the default print, p command line to match my pattern print again.
 

6.2 d command

D command to delete the entry line. sed first input line from the file to copy the pattern space, and then the sed command line execution, and finally the contents of the pattern space displayed on the screen. If you issue the command d, the current mode of input space row is deleted, it is not displayed.

sed '$ d' datafile
# Delete the last line, and the rest are displayed

sed '/ my / d' datafile
# Delete a row that contains my, and the rest are displayed
 

6.3 s command

 
sed 's / ^ My / You / g' datafile
# Command indicates the end of the g globally replaces inline, which means that if a line appears multiple My, My all are replaced with You.

sed -n '1,20s / My $ / You / gp' datafile
# Cancel the default output, the processing line in the 1-20 My matches ending row, all the My inline replaced by You, and print to the screen.
 
sed 's # My # Your # g' datafile
# Character immediately after the s command is to find and replace string delimiters between bursts. The default delimiter is a forward slash, but can be changed. No matter what character (newline except backslash), followed by s as long as the command, it becomes a new string delimiter.
 

6.4 e Options

-e is editing commands for editing the case sed to perform multiple tasks. Before the beginning of the next line editing, all the edits are applied to the pattern buffer line.

sed -e '1,10d' -e 's / My / Your / g' datafile
# -e Option is used for multiple editing. The first re-edited to delete lines 1-3. All My second re-edited to appear Replace Your. Because it is carried out both progressive editing (ie these two commands are executed on the current line pattern space), so editing commands in the order will affect the results.
 

6.5 r command

r command is a read command. sed Use this command to a text file is added to the contents of a specific location on the current file.

sed '/ My / r introduce.txt' datafile
# If the file datafile in a row to match the pattern My, you read the contents of a file introduce.txt after the row. If My line of more than one line, then after each row are read into the My Content introduce.txt file.
 
6.6 w command
sed -n '/ abcang / w me.txt' datafile
 

6.7 a \ command

a \ Append command is a command, append the new text will be added to the file behind the current line (ie, read into the pattern buffer rows). Additional text below the line is in sed command on a separate line. If you want to append the contents of more than one line, then each line must end with a backslash, except for the last line. The last line will end with quotation marks and file name.

sed '/ ^ abcang / a \
> abcang and mjfan are husband \
> And wife 'datafile
# If a match is found in the beginning of the line abcang is appended abcang and mjfan in the row below the datafile files are husband and wife
 

6.8 \ command

i \ command to insert new text in front of the current line.

 

6.9 c \ command

sed Use this command to modify an existing text into a new text.

 

6.10 n command

sed use this command to get the next line of the input file, and read into the buffer mode, any sed command will be applied to the matching line immediately next line.

 
sed '/ abcang / {n; s / My / Your /;}' datafile
NOTE: If you need to use multiple commands, or need to address nested within a certain range of addresses, you must use curly braces to enclose command, write only one command per line, or split it with a semicolon in the same row of a plurality of command.
 
6.11 y command
This command is similar to the UNIX / Linux in the tr command, characters from left to right in accordance with the one-way conversion. For example, y / abc / ABC / will all be converted into a lowercase A, converted to lowercase b B, converted to lowercase c C.
 
sed '1,20y / abcang12 / ABCANG ^ $ /' datafile
# 1-20 inner row, all lowercase to uppercase abcang, will be converted into 1 ^, will be converted into $ 2.
# Regex metacharacters for y command does not work. The separator s command, like slash can be replaced with other characters.
 

6.12 q command

q sed command will cause the program to exit, no longer carry out other processes.

sed '/ abcang / {s / abcang / ABCANG /; q;}' datafile
 

6.13 h and g command command

#cat datafile
My name is abcang.
Your name is mjfan.
abcang is mjfan's husband.
mjfan is abcang's wife.
  
sed -e '/ abcang / h' -e '$ G' datafile
sed -e '/ abcang / H' -e '$ G' datafile
# Through the above two commands, you will find that h will scratch the original contents of the buffer to clear, save only when the contents are saved into the last execution h mode space. The H command put each match hrwnag additional rows stored in a temporary buffer.
sed -e '/ abcang / H' -e '$ g' datafile
sed -e '/ abcang / H' -e '$ G' datafile
# Through the above two commands, you will find the g staging buffer contents replace the current line in the pattern space content here that is replacing the last row. The G command put the staging buffer contents appended to the pattern space after the current line. Here that is appended to the end.
 

 

7. sed script

sed script is written in a file of a sed command. Script, the end of the request command can not have any extra spaces or text. If there are multiple commands in a row, separated by semicolons. When executing the script, sed the first line of the first input file is copied to the buffer mode, and perform all of the script commands. After each line is processed, sed and then copy the file to the next line in the buffer mode, all commands perform script. When using sed scripts, no longer with quotation marks to ensure sed command is not interpreted by the shell. For example sed script script:

#handle datafile
3i \
~~~~~~~~~~~~~~~~~~~~~
3, $ s / \ (abcang \) is \ (mjfan \) / \ 2 is \ 1 /
$ A \
We will love eachother forever! !
 
 

 
#sed -f script datafile
My name is abcang
Your name is mjfan
~~~~~~~~~~~~~~~~~~~~~
mjfan is abcang's husband. # la ~ ~ ~
mjfan is abcang's wife.
We will love eachother forever! !
     
         
         
         
  More:      
 
- Database Blob data type conversion String (Programming)
- Tip: Use Cryptsetup U disk encryption (Linux)
- Using the Linux folder wc statistics number of lines of code for all files (including subdirectories) (Linux)
- After installing minimize RHEL / CentOS 7 we need to do (Linux)
- Nginx configuration support f4v video format player (Server)
- Linux file system management partition, format, mount - label mount (Linux)
- Ubuntu 14.04 Boot Repair (Linux)
- MySQL to NoSQL avatar (Database)
- Computer security protection remove local and remote system log files (Linux)
- To install and deploy PHP environment under the CentOS (Server)
- Linux System Getting Started Learning: Join cron job in Linux (Linux)
- Definition Format Oracle basis of various statements (Database)
- Linux - EXT2 file system is described in detail (Linux)
- mydumper installation error process (Database)
- Ubuntu 15.04 and CentOS 7 to deploy .NET, Mono and DNX (Server)
- Mistakenly deleted redo log file group being given the lead to start the database ORA-03113 (Database)
- PL / SQL data types (Database)
- MySQL common functions summary (Database)
- Linux Security trust with users (Linux)
- Quickly build and install Linux KVM system (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.