LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 11: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 11:52 AM.
 
Old 03-10-2011, 11: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, 12:01 PM   #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, 12:18 PM   #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 12:27 PM.
 
Old 03-10-2011, 12:29 PM   #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, 12:31 PM   #6
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,

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, 12:33 PM   #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 12:34 PM.
 
Old 03-10-2011, 08:18 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
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, 02:40 AM   #9
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
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


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


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