LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-23-2017, 08:18 AM   #1
bmxakias
Member
 
Registered: Jan 2016
Posts: 244

Rep: Reputation: Disabled
Question Clear all lines in a csf deny file that contains text: Added by Fail2Ban


Hello

I am looking for an easy way to clear all lines like:

Code:
123.456.789.000 # Added by Fail2Ban for nginx-get-f5 - Wed Aug 23 13:02:55 2017
from:
Code:
/etc/csf/csf.deny
by checking the text: Added by Fail2Ban

How can i do that with a simple command from ssh on my Centos server?

Thank you
 
Old 08-23-2017, 08:29 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,314
Blog Entries: 13

Rep: Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370
Suggest you look up the 'd' option for the sed command.
 
Old 08-23-2017, 08:32 AM   #3
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,185

Rep: Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377
Code:
 grep -v Fail2Ban /etc/csf/csf.deny > /tmp/csf.deny;mv /tmp/csf.deny /etc/csf/csf.deny
You're welcome....
 
Old 08-23-2017, 08:44 AM   #4
bmxakias
Member
 
Registered: Jan 2016
Posts: 244

Original Poster
Rep: Reputation: Disabled
I think that can be done direct to the file without creating another file in /tmp folder and so on...... ?
 
Old 08-23-2017, 08:47 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,314
Blog Entries: 13

Rep: Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370
Quote:
Originally Posted by bmxakias View Post
I think that can be done direct to the file without creating another file in /tmp folder and so on...... ?
I agree, and ...
Quote:
Originally Posted by rtmistler View Post
Suggest you look up the 'd' option for the sed command.
 
Old 08-23-2017, 08:48 AM   #6
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,185

Rep: Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377
Quote:
Originally Posted by bmxakias View Post
I think that can be done direct to the file without creating another file in /tmp folder and so on...... ?
Sure, go read rtmistler's reply and read up on sed

Edit: I never said my method was the best, simply an alternative.

Last edited by TenTenths; 08-23-2017 at 08:52 AM.
 
Old 08-23-2017, 12:55 PM   #7
bmxakias
Member
 
Registered: Jan 2016
Posts: 244

Original Poster
Rep: Reputation: Disabled
@TenTenths

No offense and thanks for your reply


@rtmistler

Thanks i will try to find a way to do it.....
 
Old 08-23-2017, 01:01 PM   #8
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,185

Rep: Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377Reputation: 1377
Quote:
Originally Posted by bmxakias View Post
@TenTenths

No offense and thanks for your reply
None taken.
 
Old 08-23-2017, 01:22 PM   #9
bmxakias
Member
 
Registered: Jan 2016
Posts: 244

Original Poster
Rep: Reputation: Disabled
Can't find a sample with sed -d option on how to do that

Any help please?

Any sample?
 
Old 08-23-2017, 01:28 PM   #10
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 633

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
A quick search like "delete matching lines with sed" will do wonders
https://duckduckgo.com/?q=delete+matching+lines+sed

http://stackoverflow.com/questions/5410757/ddg#5410784
Quote:
sed '/pattern to match/d' ./infile
 
Old 08-23-2017, 02:21 PM   #11
bmxakias
Member
 
Registered: Jan 2016
Posts: 244

Original Poster
Rep: Reputation: Disabled
Quote:
sed '/Added by Fail2Ban/d' /etc/csf/csf.deny
Like this?

Last edited by bmxakias; 08-23-2017 at 02:25 PM.
 
Old 08-23-2017, 02:26 PM   #12
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,314
Blog Entries: 13

Rep: Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370Reputation: 4370
Quote:
Originally Posted by bmxakias View Post
Like this?
Yes and try it. Because the output result will go to stdout and in fact to get it to that file, you'd need to use the -i flag for sed or pipe the result to a file. The -i flag will perform the changes "in place", however it is a great idea to see the outcome piped to stdout first to validate your syntax. I believe you may need a leading 's', as in:
Code:
's/<string>/d'
 
Old 08-23-2017, 02:29 PM   #13
bmxakias
Member
 
Registered: Jan 2016
Posts: 244

Original Poster
Rep: Reputation: Disabled
Maybe like this then?


Quote:
sed -i 's/Added by Fail2Ban/d' /etc/csf/csf.deny

Last edited by bmxakias; 08-23-2017 at 10:57 PM.
 
Old 08-25-2017, 01:22 AM   #14
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,737

Rep: Reputation: 789Reputation: 789Reputation: 789Reputation: 789Reputation: 789Reputation: 789Reputation: 789
Like the s command in sed (substitute)
there is the d command (delete).
 
Old 08-25-2017, 06:24 AM   #15
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,737

Rep: Reputation: 789Reputation: 789Reputation: 789Reputation: 789Reputation: 789Reputation: 789Reputation: 789
Quote:
Originally Posted by bmxakias View Post
Like this?
Yes like this.
Add -i option to direct the output back to the file.
Code:
sed -i '/Added by Fail2Ban/d' /etc/csf/csf.deny

Last edited by MadeInGermany; 08-25-2017 at 06:26 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Inserting Text lines in a Text file that is existing via Bashscript techjaymindave Linux - Newbie 15 03-14-2017 09:12 AM
Remove lines in a text file based on another text file asiandude Programming 10 01-29-2009 10:59 AM
Adding lines of text to beginning of a text file BillKat Programming 2 01-19-2009 10:40 AM
print a text file with long lines and line number added powah Linux - General 2 05-26-2006 02:02 PM
Grab text lines in text file LULUSNATCH Programming 1 12-02-2005 10:55 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration