Shell_04-sed basic usage

Source: Internet
Author: User

Shell_04-sed basic usage sed command sed basic usage sed Command Option sed command positioning sed basic edit sed basic usage (1) sed (stream editor) is a stream editor, you can edit text files and standard input. Sed only edits the copy of the original file in the buffer, and does not edit the original file. To save the changes, you can use the following two methods: redirect w edit command sed basic usage (2) There are three methods to call sed: Enter the command in Shell command line to call sed, format: sed [Option] 'sed command ': Enter the file, insert the sed command into the script file, and then use the sed command to call it. The format is: sed [Option]-f sed script file input file after the sed command is inserted into the script file, the most common method is to set the script file to executable, and then directly execute the script file, format :. /sed script file input file method 2 the first line of the script file does not start #! Start with/bin/sed f. The first line of the script file in the third method is #! /Bin/sed f it is recommended to use the first and third Methods sed Command Option-I directly modify the file content-n do not print all rows to the standard output-e indicates to parse the Next string to sed edit command, if only one edit command is passed to sed, the-e option can omit-f to indicate that the sed script file is being called. After sed finishes processing the text, the input file is printed to the standard output; when the-n option is added, sed no longer prints the input file, but only prints the processing result. When multiple editing commands are passed to the sed command, before each edit command, add the-e Option sed text locating method x to specify the row number x, y specifies the row number range from x to y/pattern/query the rows in the contain mode/pattern/query the rows in the two modes/pattern /, the Rows x,/pattern/From the matching row of pattern to the matching row of pattern, x, y! Query rows that do not contain rows x and y. Edit the sed command p print match rows = print the row number of the file a \ append text information after the row number is located I \ insert text information before the row number is located d Delete locate line c \ replace positioning text with new text s replace mode r read text from another file w write text into one file y transform character q first pattern match complete then exit l and display the control character {} equivalent to the octal ASCII code to read the next input line from Command Group n in the positioning line, use the next command to process the new line h. Copy the mode buffer text to the keep buffer H. append the mode buffer text to the keep buffer x swap mode buffer and keep the buffer content g. Keep the buffer content copied mode buffer G appends the buffer content to the mode buffer # cat insert. sed #! /Bin/sed-f/^ root:/I \ We insert a new line. /^ root:/a \ add a new line. (kiki)/uucp:/c \ ccie w/tmp/test.txt #. /insert. sed/etc/passwd | head sed locate the text line that matches the pattern. This is a frequently used command of the sed command and usually needs to be basically edited using the regular expression sed (1) -a: append text: insert-I after the matching row insert text: insert-s before the matching row Modify text: Replace the match text line with the new text-d to delete the text: delete sed from a specified row or a specified row range (2) use new text to replace the matched text rows. The text replacement function is similar to the text modification function, the difference between them is that replacement text can replace a string, while modification text is to modify the entire line to replace the text format: s/replaced string/New String/[replacement option] g indicates to replace all occurrences of the replaced string in the text. p is combined with the-n option, print the replace row w file name to direct the output to a file sed basic edit (3) by default, the sed s command outputs all the replaced text, to print only replace rows, use the-n and p options. The command format is as follows: sed-n's/replaced string/New String/P' input file sed basic edit (4) read text from the file, r option exit command: q indicates to exit the conversion command immediately after matching the specified address: y indicates character conversion, it converts a series of characters to the corresponding character sed 'y/the transformed Character Sequence/The 'input file sed y command requires the transformed character sequence and the transformed Character Sequence character Sequence length, otherwise, the sed y command will report an error. The {} symbol in the sed edit command can be used to specify the Command Group to be executed on the positioning line. Its role is similar to the-e Option of sed, to execute multiple editing commands [root @ I tmp] # cat passwd HAHAROOTrootroot [root @ I tmp] # sed 'y/root/kxxs/'passwd HAHAROOTkxxskxxs

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.