LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 08-09-2009, 11:20 AM   #1
asim.mcp
LQ Newbie
 
Registered: Oct 2006
Posts: 28

Rep: Reputation: 15
Unhappy search word and comment or delete


Hi,


I want to know that how can i search for a word in a file and then comment it or delete it.

I know this command works.
sed 's/word/#word/'g inputfile > outputfile

But the lines in my input file are as under:

zone "abc.com" { ---- This line can be comment
type master; ----- This line repeat again and again in the file
file "abc.txt";
allow-query { any; }; -This line repeat again and again in the file
};

I want to know how can i comment a certain zone. Any help please.


Regards
 
Old 08-10-2009, 08:38 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
show more input samples. show your final output.
 
Old 08-10-2009, 11:01 PM   #3
asim.mcp
LQ Newbie
 
Registered: Oct 2006
Posts: 28

Original Poster
Rep: Reputation: 15
input is:

Script will ask me:
Enter a domain: abc.com <- input

output - I don't want it to show me output... I want, When i enter a domain name it should search it into a file called "named.conf" and then comment it as I've done below.

//zone "abc.com" {
// type master;
// file "abc.file";
// allow-query { any; };
//};

original zone record in file "named.conf" is as under:

zone "abc.com" {
type master;
file "abc.file";
allow-query { any; };
};


Regards
 
Old 08-10-2009, 11:01 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
You might be looking for an address range.....

Suppose you wanted to start with a line containing "fred", and end with a line containing "lulu". Within this range, you want to add "#" to the beginning of each line:

Code:
sed '/fred/,/lulu/s/^/#/' oldfile > newfile
Best SED tutorial on the planet here:
http://www.grymoire.com/Unix/Sed.html
 
Old 08-11-2009, 12:56 PM   #5
asim.mcp
LQ Newbie
 
Registered: Oct 2006
Posts: 28

Original Poster
Rep: Reputation: 15
Dear Thanks
It worked for me but there is one problem
It comment from:

"zone "abc.com" {
type master;
file "abc.file";
allow-query { any; };"
}; <-- This line is still not commented..

I've given the beginning zone "abc.com" and ending };

e.g.:
sed '/zone "abc.com"/,/};/s/^/\/\//' name.conf > name.conf-new


Any help please?
 
Old 08-11-2009, 01:05 PM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
When using an address range, it will start at the first occurence of address1 and end on the first occurence of address2. In your example, you can simply change the second address to "^};" meaning "};" at the beginning of the line.
 
Old 08-11-2009, 08:01 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
input="abc.com"
awk -v input="$input" 'BEGIN{ RS=""}
$2 ~ input { 
    gsub(/\n/,"\n#")
    sub(/^/,"#")
}
{print $0 "\n"}
' file
output
Code:
# more file                                                                                                                           
zone "123.com" {
type master;
file "123.file";
allow-query { any; };
};

zone "abc.com" {
type master;
file "abc.file";
allow-query { any; };
};

zone "def.com" {
type master;
file "def.file";
allow-query { any; };
};

# ./shell.sh
zone "123.com" {
type master;
file "123.file";
allow-query { any; };
};

#zone "abc.com" {
#type master;
#file "abc.file";
#allow-query { any; };
#};

zone "def.com" {
type master;
file "def.file";
allow-query { any; };
};
or

Code:
awk -v input=$input '$0 ~ input,/^$/ { $0="#"$0}1' file

Last edited by ghostdog74; 08-11-2009 at 08:09 PM.
 
Old 08-13-2009, 07:00 AM   #8
asim.mcp
LQ Newbie
 
Registered: Oct 2006
Posts: 28

Original Poster
Rep: Reputation: 15
Dear ,


I'm so much thankful to you. What i wanted, i got.

Again many many thanks
God Bless All.
 
Old 08-15-2009, 02:20 AM   #9
asim.mcp
LQ Newbie
 
Registered: Oct 2006
Posts: 28

Original Poster
Rep: Reputation: 15
Hi Dear,,


Is there any possibility to give filename as input? because currently i've to enter domain name one by one.
 
  


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
how can delete certain word from file Barq Linux - General 11 01-06-2008 07:06 PM
To search a particular word som_kurian Programming 8 12-15-2007 03:54 PM
How can i search word in directories? Whynot Linux - General 1 08-18-2007 04:35 PM
search file contains word prospekrisal Linux - Newbie 5 07-20-2006 09:24 PM
Word search Alien_Hominid Slackware 5 04-19-2006 05:52 AM

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

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