Sed command for deleting the matched line from file

Just delete the line from access.log: We’ve one file called access.log with below mention data/line.

Command:
sed -i '/\/five-5-success-factor.html/d' access.log

access.log:
Hello tutorialbyexample, home page access by GET http://www.tutorialbyexample.com/success HTTP 200
This is the success page of five success by POST http://www.tutorialbyexample.com/2015/09/five-5-success-factor.htmlHTTP 200
Hello tutorialbyexample, home page access by GET http://www.tutorialbyexample.com/success HTTP 200
This is the success page of five success by POST http://www.tutorialbyexample.com/2015/09/five-5-success-factor.htmlHTTP 200
Hello tutorialbyexample, home page access by GET http://www.tutorialbyexample.com/
This is the success page of five success by POST http://www.tutorialbyexample.com/2015/09/five-5-success-factor.html
Hello tutorialbyexample, home page access by GET http://www.tutorialbyexample.com/

Output in same file access.log:
Hello tutorialbyexample, home page access by GET http://www.tutorialbyexample.com/success HTTP 200
Hello tutorialbyexample, home page access by GET http://www.tutorialbyexample.com/success HTTP 200
Hello tutorialbyexample, home page access by GET http://www.tutorialbyexample.com/
Hello tutorialbyexample, home page access by GET http://www.tutorialbyexample.com/

Command explanation:
sed -i '/\/five-5-success-factor.html/d' access.log

sed –i option will replace in same file. It will open for manipulations and write it in same file.

“/five-5-success-factor.html” this is the actual data want to find but we can’t use / as data so just added escape char \ for considering / as data.
And it consider as “\/five-5-success-factor.html” for finding the string.


/d for deleting the line, which matched as per find string.

Next

Ref from Unix manual.

1 comment:

  1. Such an excellent and interesting blog, do post like this with more information which is very useful for everyone.
    Salesforce Training for beginners  

    ReplyDelete