LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 03-10-2011, 10:48 AM   #1
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Rep: Reputation: 16
find opposite with grep


Hi,

I want to print all lines that doesn't match a name in bash.
I'm using grep, but my problem is that I don't know to find the opposite of I what I want.

For example, in the text:
Code:
This
is
a
string
I want to print all line that doesn't match the word 'is'.
I want to print
Code:
This
a
string
How can I do that?

Last edited by xeon123; 03-10-2011 at 10:52 AM.
 
Old 03-10-2011, 10:58 AM   #2
j1alu
Member
 
Registered: Apr 2009
Distribution: debian gnu/linux
Posts: 798

Rep: Reputation: Disabled
grep -v
so this might (!) work for you:
grep -v "\bis\b"
I am unsure about the quotes. the both \b make it being a standalone word
(else you would hit "This" too)

Don't take my word for it.
 
Old 03-10-2011, 11:01 AM   #3
siranjeevi
Member
 
Registered: May 2010
Location: India
Posts: 79

Rep: Reputation: 7
Hi,


The -v option will exclude the matchs,

cat filename | grep -v is
 
Old 03-10-2011, 11:18 AM   #4
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
I'm not asking correctly.
This is my problem:

I've a tmp.txt file with this content:
Code:
My
name
is/was
test
And I'm using the following script:

Code:
cat tmp.txt | while read line
do
            if [[ $line != "^is" ]]
            then
                echo $line                                                  
            fi;
     
done
but the problem is that if clause is not excluding the "is/was" word. What's wrong?

Last edited by xeon123; 03-10-2011 at 11:27 AM.
 
Old 03-10-2011, 11:29 AM   #5
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
I've found the solution:

Code:
cat tmp.txt | while read line
do
            if [[ $line != is* ]] // removing the ""
            then
                echo $line                                                  
            fi;
     
done
 
Old 03-10-2011, 11:31 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

The example posted in post #4 works on my side:
Code:
$ cat infile
My
name
is
test

$ cat foo.sh
 #!/bin/bash

cat infile | while read line
do
  if [[ $line != "is" ]]
  then
    echo $line                                                  
  fi
done

$ ./foo.sh
My
name
test
Are you sure your infile is a linux file?
file infile should give you infile: ASCII text and not infile: ASCII text, with CRLF line terminators. Use dos2unix infile to reformat from dos/windows to unix.

Hope this helps.

Original post was edited while replying....
 
Old 03-10-2011, 11:33 AM   #7
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Another way to do it is with sed:

sed '/^is/ d' tmp.txt

will delete lines beginning with "is". If you want to change the file in place, you can use sed's -i option. A tutorial for sed can be found here.

Last edited by Nylex; 03-10-2011 at 11:34 AM.
 
Old 03-10-2011, 07:18 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Hey druuna ... You missed the updated input file (is now is/was) and the carat (^) in front of 'is' in the script.

OP - I agree with Nylex that sed or grep would be much simpler, unless of course you are requiring to do more in your script
 
Old 03-11-2011, 01:40 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by grail View Post
Hey druuna ... You missed the updated input file (is now is/was) and the carat (^) in front of 'is' in the script.
Hey grail.... I noticed, hence the remark at the bottom of my post.
 
  


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
using find or grep to find a group of text strings. tkmsr Linux - Newbie 3 03-04-2011 07:02 AM
opposite of grep 3saul Linux - Software 8 04-21-2010 01:29 AM
Find/grep/wc command to find matching files, print filename and word count dbasch Linux - Newbie 10 09-14-2009 05:55 PM
using find /grep gamor Linux - Newbie 14 04-13-2005 02:36 PM
help me find grep-2.5.1 shanenin Linux - Software 1 01-23-2004 09:01 PM

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

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