Reproduced Shell deletes scripts for various annotations

Source: Internet
Author: User

Transferred from: http://www.cppblog.com/zhangyq/archive/2010/10/08/127915.html

1.txt content:
File content
Aabbcc<<<comment Part 1
Abcdefg
Hilkdifdfdf
Mmmmmmmm
Eeeeeeeeeeeeee
Comment Part 2>>>
Ddeeff
SED-E ": Begin; /<<</,/>>>/{/>>>/! { $! {N; b begin}; }; s/<<<.*>>>//; };" 1.txt
File content
Aabbcc
Ddeeff

    • The first curly brace {} Represents the beginning of the command block, like the C syntax, which is no longer spoken.

    • :begin, this is a label, man is called the label, which is the jump tag, for the B and T command, in this case, the B command is used.

    • /<<</,/>>>/, this is an address range (Addresses), and the commands in the following {} are used only for content between the address ranges. Where the previous part of the comma is the start address, and the comma is followed by the end address, both are regular expressions. Because SED is "stream" line processing, the end address can be omitted, that is, if the end range of the address does not exist, it will be processed until the end of the file. The address range used in this example is mainly to reduce the amount of data processed, because although the processing of a row is extended to multiple rows with the n command, the string to be processed in buffer can be very long and inefficient if it is extended from the beginning of the file to <<< until it appears. So you can get the right result out of this processing range,

    • Like what:

      $ sed -e ":begin; { />>>/! { $! { N; b begin }; }; s/<<<.*>>>/COMMENT/; };" testor$ sed -e "{:begin;  />>>/! { $! { N; b begin }; }; s/<<<.*>>>/COMMENT/; };" test
    • />>>/!, >>> is to replace the end tag of the content, with the ! meaning that when a line is finished, if no end tag is found ...

    • $!, which $ represents the end of the string in the regular, represents the last line of the file in the SED, and this sentence, combined with the previous sentence, means: If the end tag is not found in the bank and the currently scanned row is not the last line of the file.

    • N;, after appending the contents of the next line (append) to the buffer (pattern), in our example, when the contents of this line are processed, it is aabbcc<<<comment part 1 executed here, and then the contents of the next line are put comment part 2>>> into the buffer, equivalent to "merge" into a row (the SED buffers contain only one line of content by default).

    • b begin, since the end tag is still not found <<< (note that the previous buffer has not been processed), jump back here to the label begin and start the command again. If more than one line is spaced between the start and end tags, multiple jumps occur.

    • s/<<<.*>>>/comment/; , finally, />>>/! No longer matches the success, that is, we have found the end tag, then use the S command to replace. If the start and end tags are on one line, they will go over the complex processing above and execute directly here

      If you want to delete the comment in the label C: Use the following command
      Sed-e ": Begin; { /\*\//! { $! {N; b begin}; }; s/\/\*.*\*\///; };" 2. txt
      2.txt content is as follows:
      file content
        aabbcc/*comment Part 1
      ABCDEFG
      hilkdifdfdf
      Mmmmmmmm
      Eeeeeeeeeeeeee
        Comment Part 2*/
        Ddeeff
      Execute command results as follows:
      file content
        AABBCC
        Ddeeff
       sed '/\/\*/{/\*\//d;:a; n;/\*\//d;ba};s,//.*, ' 2.txt will delete the AABBCC line above
      The above command is appropriate for annotating multiple lines, the code and Comment lines are not on one line, with sed-e ": Begin; { /\*\//! { $! {N; b begin}; }; s/\/\*.*\*\///; }; "causes a blank line to appear
      if the following is not a problem
      file content
        AABBCC
      /*comment Part 1
      ABCDEFG
      HILKDIFDFDF
      Mmmmmmmm
      Eeeeeeeeeeeeee
        Comment Part 2*/ddeeff
      perform sed-e ": Begin; { /\*\//! { $! {N; b begin}; }; s/\/\*.*\*\///; The result is
      file content
        AABBCC
       ddeeff
      But there is still a blank line, so if you want to remove the C comment with sed-e ": Begin; { /\*\//! { $! {N; b begin}; }; s/\/\*.*\*\///; }; "more appropriate, then delete empty lines on the line

    • sed/^$/d filename Delete empty line, the actual application can change the-e parameter to-I, so that you can directly manipulate and modify the source file;

      Here's a script that removes the comments # of various C and C + + and shell scripts themselves
      #delete the comment line begin with '//comment '
      Sed-i "/^[\t]*\/\//d" $filename
      #delete the Commnet line end with '//comment '
      Sed-i "s/\/\/[^\"]*//"$filename
      #delete the comment only occupied a line '/* commnet */'
      Sed-i "s/\/\*.*\*\///" $filename
      #delete the comment that occupied many lines '/*comment
      # *comment
      #                                              */
      Sed-i "/^[\t]*\/\*/,/.*\*\//d" test.conf
      Sed-i ' s#\#.*# # ' $filename
      Sed-i ": Begin; { /\*\//! { $! {N; b begin}; }; s/\/\*.*\*\///;  };" $filename
      Sed-i '/^$/d ' $filename

Reproduced Shell deletes scripts for various annotations

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.