LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 09-22-2009, 07:01 PM   #1
coolplanet
LQ Newbie
 
Registered: Apr 2009
Posts: 20

Rep: Reputation: 0
Search for ip address in file and remove it


Dear sir,

I have a lot of files in the server with name like checkfile.jsp and in the file has two or three lines with an ip address xxx.xxx.xxx in it. I wish to know if there any command to grep for the ip address and remove it
in file but keep the " ", eg "xxx.xxx.xxx"
 
Old 09-22-2009, 08:15 PM   #2
blues1207
LQ Newbie
 
Registered: Sep 2009
Posts: 7

Rep: Reputation: 0
man sed
 
Old 09-22-2009, 09:08 PM   #3
quanta
Member
 
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724

Rep: Reputation: 101Reputation: 101
Some suggestions for you: http://www.linuxquestions.org/questi...check.-219187/

What you need is take a Regex (Regular Expression) book and read it.
 
Old 09-23-2009, 01:45 AM   #4
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by coolplanet View Post
Dear sir,

I have a lot of files in the server with name like checkfile.jsp and in the file has two or three lines with an ip address xxx.xxx.xxx in it. I wish to know if there any command to grep for the ip address and remove it
in file but keep the " ", eg "xxx.xxx.xxx"
So you want to keep everything in the file but this particular IP address, yes? I detect the distinct aroma of homework. Here is a hint:

Code:
$ man sed
 
Old 09-24-2009, 06:34 PM   #5
coolplanet
LQ Newbie
 
Registered: Apr 2009
Posts: 20

Original Poster
Rep: Reputation: 0
Sed command is to do within a single file only. I have hundreds of files which has this same ip inside. Can i use something like this find "xxx.xxx.xx" -exec rm {} \; ?
 
Old 09-25-2009, 12:02 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
sed takes wildcards for file spec
http://www.grymoire.com/Unix/Sed.html

and you can do in-place editing
http://linux.die.net/man/1/sed
 
Old 09-25-2009, 12:49 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by coolplanet View Post
Sed command is to do within a single file only. I have hundreds of files which has this same ip inside. Can i use something like this find "xxx.xxx.xx" -exec rm {} \; ?
No. The find command finds files by their properties, not by their contents. You could use find to find the files by name and then sed to edit them.
 
Old 09-25-2009, 01:12 AM   #8
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by coolplanet View Post
Sed command is to do within a single file only. I have hundreds of files which has this same ip inside. Can i use something like this find "xxx.xxx.xx" -exec rm {} \; ?
I can't create an example right now, but:

1. Use "find" to create a list of target files.
2. Apply the list to a "sed" filter that reads the original file and places the result in a temp file.
3. Copy the temp file over the original.
4. Continue looping over file paths.

But don't try to sed from a particular file directly to the same file:

sed "regex" < file > file

This is a bad mistake. It sometimes works while you are testing, then when you turn your back, it starts eating files. Always use a temp file for the output of the pipe.
 
Old 09-27-2009, 07:06 PM   #9
coolplanet
LQ Newbie
 
Registered: Apr 2009
Posts: 20

Original Poster
Rep: Reputation: 0
can i do like this :

grep "xxx.xxx.xxx" . > /tmp/ip.txt
for i in `cat /tmp/ip.txt`
do
sed -i -e 's/xxx.xxx.xxx/ /g' $i
done

The above will directly edit the actual file.
I am not sure how is 'not to sed from a particular file directly to the same file'
 
Old 09-28-2009, 03:05 AM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by coolplanet View Post
can i do like this :

grep "xxx.xxx.xxx" . > /tmp/ip.txt
for i in `cat /tmp/ip.txt`
do
sed -i -e 's/xxx.xxx.xxx/ /g' $i
done

The above will directly edit the actual file.
I am not sure how is 'not to sed from a particular file directly to the same file'
No.
Code:
grep "xxx.xxx.xxx" . > /tmp/ip.txt
tells grep to search the current directory and will put nothing in /tmp/ip.txt. You could have tried it and found that.

If you omit the -i from the sed command then it will write its output to stdout and you can direct it to another file. Good idea while you are testing the method
Code:
sed -e 's/xxx.xxx.xxx/ /g' <input file name> > <output file name>
 
  


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
Can you make search ...search a string in a link....a url...a web address aus9 LQ Suggestions & Feedback 4 04-16-2008 09:37 AM
search and replace ip address ? cdm33 Programming 4 06-29-2007 01:13 PM
Is there a way to search for and remove dead symbolic links? HGeneAnthony Linux - General 5 01-19-2007 02:09 AM
SUSE 9.3 YAST Install Remove search problem riba43 SUSE / openSUSE 2 07-17-2005 12:47 PM
remove color after word search in vi editor dylan912 Linux - Newbie 1 12-10-2004 04:15 AM

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

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