Shell's sed command to delete a variable with a "date time and so on" method

Source: Internet
Author: User
Tags time and date

Shell's sed command to delete a variable with a "date time and so on" method


The SED command deletes the specified content and may be known, for example, if you want to delete the line containing "Badboy" in a file, use the following command directly:

Sed-i '/badboy/d ' file.txt

You can delete all the contents of Badboy contained in the Flie.txt file.


The SED command deletes the specified line, and if the matching character is substituted with a variable, the variable contains the "/" symbol, which is handled using the following method;

For example:

[Email protected]]# VI a.txt

/usr/sbin/restart.sh

/usr/sbin/control.sh


[Email protected]]# VI del.sh

A= '/usr/sbin/control.sh '

Sed-i "/$A/d" a.txt


Execution./del.sh after the discovery did not delete, this is because the variable has "/" such special characters, the SED when the implementation of the problem occurs.

VI Modifying DEL.SH content

Escape/usr/sbin/control.sh//through "\"

Change it to this:

A= ' \/usr\/sbin\/control.sh '

Sed-i "/$A/d" a.txt

Execute the script, the./del.sh can be erased;


The following is a variable with time and date, such as the log, with the time format of "date +%d/%b/%y:%h:%m:%s"

We want to delete the contents of all lines with "12/may/2017";

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M02/96/3B/wKioL1keaHvSOahrAAGJQVLiB7A950.png-wh_500x0-wm_ 3-wmp_4-s_877379392.png "title=" Figure 1.png "alt=" Wkiol1keahvsoahraagjqvlib7a950.png-wh_50 "/>

Of course you can use:

Sed-i '/12\/may\/2017/d ' z512.txt

To delete directly, but the time is changing, so that only the specified date log content can be deleted, sometimes need to use variables, then you need the following method to implement. (e.g. today is May 19, we will delete the contents of the May 12)

First edit a script file: VI zzz.sh


Datelog= ' cat z512.txt |awk ' {print $4} ' |awk-f ' [' ' {print $} '

For Datex in $datelog

Do

Echo $datex

Done

Culdate= ' Date +%d/%b/%y:%h:%m:%s '

delday= ' date + '%d\/%b\/%y '-D " -7day"

Echo $culdate

Echo $delday

Sed-i "/$delday/d" Z512.txt


After the execution:./zzz.sh

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M00/96/3B/wKioL1kebJnRovv5AADh05UjDkw084.png-wh_500x0-wm_ 3-wmp_4-s_437586281.png "title=" Figure 2.png "alt=" Wkiol1kebjnrovv5aadh05ujdkw084.png-wh_50 "/>


All lines, including "12/may/2017" content, have been completely erased.


Note: The format of the red part variable and the use of symbols are very important, otherwise the specified content cannot be deleted correctly.

This method can be used to specify time cleanup, such as a log before a point in time.




This article is from the "boguan thick, about thin hair" blog, please be sure to keep this source http://i6313.blog.51cto.com/208364/1927504

Shell's sed command to delete a variable with a "date time and so on" method

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.