Linux Three Musketeers--sed command brief

Source: Internet
Author: User
Tags processing text

Linux Three Musketeers--sed
13.1 sed Introduction

  1. Stream Editor (sed--)
  2. SED features vs. version: processing text files, logs, configuration files
    Add, delete, modify, query
    Sed--version
  3. Syntax format: sed [options] [sed directive] [input file]
    Sed-i.bak ' S#boy#girl#g ' oldboy.txt
    Parameters of the-i sed command
    s sed command
    g Small tail/decoration
  4. sed command execution process
    13.2 SED Command Function example
  5. equals ' = ' displays line numbers sed = person.txt
  6. Single row-Enter line number to Sed-n ' 3p ' person.txt

    103,alex,coo

  7. Displays multiple lines sed-n ' 3,6p ' person.txt
  8. Line Sed-n '/oldboy/p ' person.txt containing Oldboy

    101,oldboy,ceo

  9. Show lines containing Oldboy to display yy sed-n '/oldboy/,/yy/p ' person.txt

    101,oldboy,ceo
    102,zhangyao,cto
    103,alex,coo
    104,yy,cfo

  10. Show lines containing Oldboy or yy sed-nr '/oldboy|yy/p ' person.txt
  11. Show discontinuous lines Sed-nr ' 1p;3p;4p ' person.txt
  12. Add two lines to the last line of the file sed ' $a 106,wuxingge,ufo\n107,lee,xo ' person.txt

    #a ==append Append
    #i ==insert Insertion

    Echo-e ' 106,wuxingge,ufo\n107,lee,xo ' >>person.txtcat >>person.txt <<eof

    106,wuxingge,ufo
    107,lee,xo
    Eof

  13. Do not show blank line Method! means take anti sed '/^$/d ' person.txtsed-n '/^$/!p ' Person.txtawk '!/^$/' person.txtgrep-v ' ^$ ' person.txt
  14. ' s### ' means that the first object that meets the criteria is replaced by line one sed ' s#1#dou# ' person.txt

    Dou01,oldboy,ceo
    -I means OK to modify
    -i.bak means to back up and modify first
    Double quotes inside the SED variable can be executed

  15. The & symbol in the SED command represents the contents of the previous regular match to the Echo ' 123456 ' |sed ' s#.*#<&> #g '

    <123456>

    Echo ' 123456 ' |sed-r ' s# ([0-9]) #<\1> #g '

    <1><2><3><4><5><6>

    Echo ' 123456 ' |sed ' s#.#<&> #g '

    <1><2><3><4><5><6>
    13.3 Batch modification of file names
    Method 1-Replace with SED

    LS . Jpg|sed-r ' s# (.) JPG#MV & \1avi#g '

    MV Oldboy01.jpg Oldboy01.avi
    MV Oldboy02.jpg Oldboy02.avi

    LS . Jpg|sed-r ' s# (.) JPG#MV & \1avi#g ' |bash

    Method 2-Use the Rename method to bulk modify file names

    Rename jpg avi old*.jpg

    Rename from to File
    Replace what replaces the content with what files
    13.4 Common pits using SED commands

  16. -R and-I use simultaneously:
    -R parameter in front:-ri normal result
    The-i parameter is in front:-ir indicates that the file is backed up XXR before replacing the contents of the file
  17. -R and-n are used simultaneously: the contents of the source file are emptied to show only eligible content

Linux Three Musketeers--sed command brief

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.