LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 12-26-2010, 04:47 AM   #1
Alans07
LQ Newbie
 
Registered: Mar 2009
Distribution: Ubuntu 9.04
Posts: 11

Rep: Reputation: 0
A Script to Remove lines without Chars


Hello,

I'm not good at scripting at all.
I have a text file that each contains either a domain or an IP, like this:

Code:
[me@server ~]# cat file1 
122.foo.com
yahoo.com
23345229.com
121foo.com
192.168.2.2
10foo.com
92.92.1.0
I want to remove all IPs in that file and keep others, so the result be like:

Code:
[me@server ~]# cat file2
122.foo.com
yahoo.com
23345229.com
121foo.com
10foo.com
Your help is highly appreciated.

regards.

Last edited by Alans07; 12-26-2010 at 06:14 AM. Reason: use code tags.
 
Old 12-26-2010, 04:56 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
Have a look at grep, sed or awk and you should easily find a solution
 
Old 12-26-2010, 06:17 AM   #3
Alans07
LQ Newbie
 
Registered: Mar 2009
Distribution: Ubuntu 9.04
Posts: 11

Original Poster
Rep: Reputation: 0
grail, the reason I posted here is because I couldn't find any solutions.
I was hoping someone done this before or know how to do it and share the info.
 
Old 12-26-2010, 06:26 AM   #4
Alans07
LQ Newbie
 
Registered: Mar 2009
Distribution: Ubuntu 9.04
Posts: 11

Original Poster
Rep: Reputation: 0
Code:
> cat file1 | grep [a-z] > file2
I can't believe it was so simple.

thanks anyway.
 
Old 12-26-2010, 06:27 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,
Assuming that all none IP entries have at least one letter:

grep "[a-zA-Z]" file1 > file2

Or, targeting the IP numbers:

grep -v "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" file1 > file2

Hope this helps.
 
1 members found this post helpful.
Old 12-26-2010, 08:46 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
Quote:
I can't believe it was so simple.
This is why i only suggested the tools and not the solutions.
 
Old 12-26-2010, 09:07 AM   #7
vonbiber
Member
 
Registered: Apr 2009
Distribution: slackware 14.1 64-bit, slackware 14.2 64-bit, SystemRescueCD
Posts: 422

Rep: Reputation: 86
Quote:
Originally Posted by Alans07 View Post

Code:
[me@server ~]# cat file1 
122.foo.com
yahoo.com
23345229.com
121foo.com
192.168.2.2
10foo.com
92.92.1.0
I want to remove all IPs in that file and keep others, so the result be like:

Code:
[me@server ~]# cat file2
122.foo.com
yahoo.com
23345229.com
121foo.com
10foo.com
Code:
sed '/^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/d' \
    file1 > file2
or (if you don't want to keep the original file
Code:
sed -i '/^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/d' \
    file1

Last edited by vonbiber; 12-26-2010 at 09:08 AM.
 
1 members found this post helpful.
  


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
remove duplicate lines from shell script himu3118 Programming 16 04-21-2012 08:37 AM
script to remove the lines which are having the duplicate value in 2 fields ajcapri Linux - Newbie 10 11-29-2009 10:38 PM
How to remove first 2 lines of a file in a script nazs Programming 16 02-19-2007 08:08 AM
Make a script remove lines from a file? spiffytech Linux - Software 5 12-29-2005 12:50 PM
How to remove or strip control chars in the 'script' utility? fieldmethods Linux - General 1 10-24-2003 02:11 PM


All times are GMT -5. The time now is 07:51 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration