Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ sed command     - Android using shape drawable material production (Programming)

- Ansible installation configuration and simple to use (Server)

- MySQL multi-master-slave synchronization (Database)

- Tor Browser: under Linux for anonymous Web browsing ultimate browser (Linux)

- Debian 7 and Debian 8 users how to install Oracle Java 8 (Linux)

- Java-- get the reflection object information (Programming)

- See how --nand flash timing diagram of a read operation Comments (Programming)

- Linux6.1 / 6.5 dual-NIC Binding (Linux)

- Repair Raspbian Encountered a section with no Package (Linux)

- Linux system security (Linux)

- OpenWRT environment to build (Linux)

- Java and C / C ++ data conversion when network communication (Programming)

- Linux security configuration (Linux)

- ORA-38856: Unable instance UNNAMED_INSTANCE_2 (redo thread 2) marked enabled (Database)

- Learning OpenCV: (VS2010-openCV2.4.3-win7 configuration instructions) (Linux)

- Install FFmpeg compiling from source in Mac OS X environment (Linux)

- Linux Shell debugging (Programming)

- Linux file system (inode and block) (Linux)

- JavaScript is implemented without new keywords constructor (Programming)

- OpenSSL to generate public and private key (Linux)

 
         
  sed command
     
  Add Date : 2018-11-21      
         
         
         
  Outline

sed is a stream editor for short, it is the stream editor. It is a line after first treatment, handling, storing the row currently being processed in a temporary buffer, called "model space" (pattern space), the contents of the buffer and then treated with sed command processing is complete, the buffer content sent to the screen. Then the next line, which is repeated until the end of the file. Content of the document does not change, unless you use redirection to store the output.

Use the syntax

Usage Rules sed command like this:

sed [option] 'command' input_file
Which option is optional, commonly used option are the following:

-n Use quiet (silent) mode (can not imagine why not -s). Sed in general usage, all content from stdin generally will be listed on the screen. But if you add -n parameter is only after special treatment sed row (or action) will be listed;
sed -e operation performed directly on the command line editing mode;
sed -f direct actions in a written document, -f filename command filename sed you can perform within;
Let sed -r command supports extended regular expression (default is the basis of regular expressions);
-i directly modify the contents of the file to read, instead of the output from the screen.

Frequently used commands are the following:

a \: additional line (append), a \ followed by the string of s (multi-line strings can \ n delimited), then add the string s are currently selected in the back row;?

c \: replace / replace the line (change), c \ followed by the string s (multi-line strings can \ n separated), it will be replaced with the currently selected row to a string s;
i \: Insert Row (insert), i \ followed by the string s (multi-line strings can \ n delimited), then in front of the currently selected row is inserted string s;
d: Delete Row (delete), the command will delete the currently selected row;
p: print print, the command will print the currently selected line on the screen;
s: replacement string (subs), usage generally s command is this: 1,2s / old / new / g, will replace the old string into a new string

Command Example

Suppose you have a local file test.txt, document reads as follows:

[testabc @ winwill ~] $ cattest.txt
thisis first line
thisis second line
thisis third line
thisis fourth line
this fifth line
happy everyday
end
This section uses the file detailed demonstration of the use of each command.

a command (append rows)

Example One

[testabc @ winwill ~] $ sed'1a \ add one'test.txt
thisis first line
add one
thisis second line
thisis third line
thisis fourth line
thisis fifth line
happy everyday
end
In this example the command section 1 for the first row, the second row is written the same 2, written in the first row to the third row 1,3, with $ the last line, such as 2, $ represents the middle of the second row to the last row All lines (including the second and last line).

In this case is to increase the role of the string "add one" after the first line, you can see the output from the specific effects.

Example Two

[testabc @ winwill ~] $ sed'1, $ a \ add one'test.txt
thisis first line
add one
thisis second line
add one
thisis third line
add one
thisis fourth line
add one
thisis fifth line
add one
happy everyday
add one
end
add one
This example shows the first and last lines are plus all the back row "add one" string from the output can be seen effect.

Example Three

[testabc @ winwill ~] $ sed '/ first / a \ add one'test.txt
thisis first line
add one
thisis second line
thisis third line
thisis fourth line
thisis fifth line
happy everyday
end
This example shows the back of the line that contains "first" string with the string "add one", you can see the output from the first row contains the first, so after the first line adds "add one"

Example Four

[testabc @ winwill ~] $ sed '/ ^ ha. * Day $ / a \ add one'test.txt
thisis first line
thisis second line
thisis third line
thisis fourth line
thisis fifth line
happy everyday
add one
end
This example uses regular expressions to match lines, ^ ha. * Day $ indicate the beginning ha to to day at the end of the line, you can match the "happy everyday" this file, so the back of the line to increase the "add one" character string.

i command (Insert row)

i command and use a command like, just in front of the row into matching string, directly above a sample of a command to replace i, this is not long-winded.

c command (replace? row)

Five cases

[testabc @ winwill ~] $ sed '$ c \ add one'test.txt
thisis first line
thisis second line
thisis third line
thisis fourth line
thisis fifth line
happy everyday
add one
This example represents the last line is replaced with a string "add one", you can see from the output effect.

Six cases

[testabc @ winwill ~] $ sed'4, $ c \ add one'test.txt
thisis first line
thisis second line
thisis third line
add one
Replace the contents of this case will be the fourth line to the last line to the string "add one".

Seven cases

[testabc @ winwill ~] $ sed '/ ^ ha. * Day $ / c \ replace line'test.txt
thisis first line
thisis second line
thisis third line
thisis fourth line
thisis fifth line
replace line
end
At the beginning of this case will ha, to the end of the day the line replace "replace line".

d command (delete? row)

Eight cases

[testabc @ winwill ~] $ sed '/ ^ ha. * Day $ / d'test.txt
thisis first line
thisis second line
thisis third line
thisis fourth line
thisis fifth line
end
In this example, delete ha beginning to the end of the day the line.

Example 9

[testabc @ winwill ~] $ sed'4, $ d'test.txt
thisis first line
thisis second line
thisis third line
This example removes the fourth line to the last line of content.

p command (print line)

10 cases

[testabc @ winwill ~] $ sed-n '4, $ p'test.txt
thisis fourth line
thisis fifth line
happy everyday
end
Printed on the screen in this case the fourth line to the last line, p is the general command and the -n option.

Example eleven

[testabc @ winwill ~] $ sed-n '/^ha.*day$/p'test.txt
happy everyday
In this example, the print ha beginning to the end of the day the line.

s command (replace string)

The practical application of s imperative to the most commonly used.

12 cases

[testabc @ winwill ~] $ sed's / line / text / g'test.txt
thisis first text
thisis second text
thisis third text
thisis fourth text
thisis fifth text
happy everyday
end
This example will replace the file all line into text, the final g is global meaning, that is, global replace, if not g, then the Bank will replace the first line.

13 cases

[testabc @ winwill ~] $ sed '/ ^ ha. * Day $ / s / happy / very happy / g'test.txt
thisis first line
thisis second line
thisis third line
thisis fourth line
thisis fifth line
very happy everyday
end
This example first match starts ha, to the end of the day the line, in this case matched to the line is "happy everyday" Thus, then replace the line happy to very happy.

Example fourteen

[testabc @ winwill ~] $ sed's / \ (. * \) Line $ / \ 1 / g'test.txt
thisis first
thisis second
thisis third
thisis fourth
thisis fifth
happy everyday
end
This example is fairly complex, to break it down. First s command mode s / old / new / g so, so old part of this example, that \ (. * \) Line $, sed command with the \ (\) contents of the package a regular n-expression part number from 1 to calculate, in this case only a \ (\), so \ (. * \) represents the first part of a regular expression that matches any part of the string, so \ (. * \) line $ match It is to the line at the end of any row. Then matching rows to replace the first part of the regular expression (in this case corresponds to line deleting section), use \ 1 represents a match to the first part of the same \ 2 for the second part, \ 3 represents the third part, You can turn this reference. For example, the following example:

[testabc @ winwill ~] $ sed's / \ (. * \) Is \ (. * \) Line / \ 1 \ 2 / g'test.txt
this first
this second
this third
this fourth
this fifth
happy everyday
end
Is part of the regular expression can be used on both sides of \ 1 and \ 2 indicates, in fact, the role of this example is to remove the intermediate portion is.
     
         
         
         
  More:      
 
- MySQL root password reset under CentOS (Database)
- Getting Started with Linux system to learn: how to check in a package is installed on Ubuntu (Linux)
- Linux process management related content (Linux)
- Install the latest Eclipse IDE in Ubuntu (Linux)
- Linux user login and IP restrictions (Linux)
- Ubuntu install Eclipse for C / C ++ and related configuration (Linux)
- ASP.NET 5 is connected with the Redis server on the Linux platform (Server)
- Install JDK 1.7 + Eclipse in CentOS 6.4 in (Linux)
- Database Blob data type conversion String (Programming)
- Ubuntu install virtual machine software VirtualBox 4.3.10 (Linux)
- Android Studio quick overview of Gradle (Programming)
- Binary tree traversal recursive and non-recursive (cyclic) traversal achieve (Programming)
- Nginx is used to build the cache module srcache_nginx (Server)
- Chromium Install Flash Official Guide (Linux)
- Getting Started with Linux system to learn: how to configure a static IP address for CentOS7 (Linux)
- The difference Docker save and export commands (Linux)
- Into the Java keyword instanceof (Programming)
- Android in the coordinate system and a method to obtain the coordinates (Programming)
- MongoDB upgrade to 2.6 (Database)
- Ubuntu 14.10 install KDE Plasma 5.2 (Linux)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.