LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-30-2012, 03:50 PM   #1
rhbegin
Member
 
Registered: Oct 2003
Location: Arkansas, NWA
Distribution: Fedora/CentOS/SL6
Posts: 381

Rep: Reputation: 23
use grep to remove file pattern from conf file


I am trying to figure out how to remove this from config files:


the data in my file to get rid of is:

allow members of "remote"
range 192.168.1.0
filename "SYSTEM33E14"

I am trying to get rid of the complete line with:
--> filename "SYSTEM33E14"

I can comment out the line, however I am running into problems trying to match this to remove it?

Last edited by rhbegin; 11-30-2012 at 04:17 PM.
 
Old 11-30-2012, 04:40 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
I think maybe SED is the better choice---something like this:
Code:
sed -n '/SYSTEM33/!p' filename > newfilename
replace SYSTEM33 with whatever is required to give an unambiguous result.
 
Old 11-30-2012, 04:50 PM   #3
rhbegin
Member
 
Registered: Oct 2003
Location: Arkansas, NWA
Distribution: Fedora/CentOS/SL6
Posts: 381

Original Poster
Rep: Reputation: 23
Quote:
Originally Posted by pixellany View Post
I think maybe SED is the better choice---something like this:
Code:
sed -n '/SYSTEM33/!p' filename > newfilename
replace SYSTEM33 with whatever is required to give an unambiguous result.
I need to remove the entire line completely, I tried this and it seemed to work.


sed '/filename/d' myfile.cfg > file.cfg

and it removed the line completely

I am still learning on this, is there a way when you use 'vi' inside the file to remove this?

thank you for the reply
 
Old 11-30-2012, 05:10 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Using the delete (d) command is equivalent to the form I showed. The real issue is to be sure that the address phrase is unambiguous.

I'm not familiar with vi, but I think it has similar capabilities.
 
Old 12-02-2012, 01:12 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

The -v switch in grep reverses the match to exclude any lines containing the expression.

Code:
grep -v "SYSTEM33E14" infile.txt >outfile.txt
As pixellany said, in vi/vim, the command would be similar to the sed syntax, although you need to specify "g" (global) as well:

Code:
:g/SYSTEM33E14/d
Incidentally, vi[m] can be used directly in scripting. If you run it with the -e and -s options, then you can pass it any EX mode commands from stdin.

Code:
printf '%s\n' 'g/SYSTEM33E14/d' 'w outfile.txt' | vim -es infile.txt
('w' with no arguments will write the changes back to the original file, naturally.)

Although for this kind of thing the lighter ed is usually recommended instead.

Code:
printf '%s\n' 'g/SYSTEM33E14/d' 'w outfile.txt' | ed -s infile.txt
How to use ed:
http://wiki.bash-hackers.org/howto/edit-ed
http://snap.nlc.dcccd.edu/learn/nlc/ed.html
(also read the info page)

And just to round out the list, here's an awk solution:

Code:
awk '! /SYSTEM33E14/' infile.txt >outfile.txt
 
  


Reply



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] grep two pattern from a file Sattee Linux - General 18 06-13-2012 04:13 AM
[SOLVED] how to extract a 2-line pattern from a file using awk, grep, etc. dcsmayei Linux - Newbie 9 06-09-2012 08:32 AM
what happens when the file in which grep is searching for pattern is blank? ameylimaye Linux - Newbie 4 10-01-2011 02:10 AM
[SOLVED] Using Grep with Pattern File and PCRE bigbot Linux - Newbie 11 10-01-2010 09:33 AM
Grep pattern first line of a file ericcarlson Linux - Newbie 11 07-20-2004 10:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:58 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