alternate for "grep -vf" command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting alternate for "grep -vf" command
# 1  
Old 07-23-2010
Question alternate for "grep -vf" command

I have been using grep for removing lines of a particular pattern from a long file in the following way

grep -vf Exclude_Lines File1 > File2

But grep seems to have some problems because sometimes it does the job, sometimes it does not....I have seen AWK is more powerful..
Can the same thing be done by using AWK?

If there is a solution, please suggest....Smilie
# 2  
Old 07-23-2010
awk
Code:
# print only lines which match regular expression (emulates "grep")
 awk '/regex/'

 # print only lines which do NOT match regex (emulates "grep -v")
 awk '!/regex/'


# awk '!/(firstexp|secondexp)/'  fileone > filetwo

sed
Code:
# print only lines which match regular expression (emulates "grep")
sed -n '/regexp/p'           # method 1
sed '/regexp/!d'             # method 2
# print only lines which do NOT match regexp (emulates "grep -v")
sed -n '/regexp/!p'          # method 1, corresponds to above
sed '/regexp/d'              # method 2, simpler syntax

# 3  
Old 07-23-2010
Quote:
Originally Posted by ananyob
grep seems to have some problems because sometimes it does the job, sometimes it does not
Unlikely, I daresay - would you care to proof (read: show us "working" vs. "failing" data)?
# 4  
Old 07-23-2010
Thanks IKON....... actually i just figured out what grep is doing.....its not the fault of grep
I have a file where 19224 lines are there with lines like (giving a snapshot)
1 1 1 2
2 2 2 3
3 3 3 4

In the file where I have mentioned all the patterns using which I need to remove selected lines containing these patterns, I have mentioned
77 78
in order to remove the line where the 3rd and 4th columns are 77 and 78.
The line which I wanted to remove looks like
81 5 77 78
But in the process, GREP is removing the following lines apart from the one (mentioned above) that I wanted to remove.
8373 9 7877 7878
8374 9 7877 7879
8375 9 7877 7880

Because its finding 77 and 78 next to each other according to my pattern, its removing these lines. Is there a better way to tackle this problem rather than mentioning the entire line as "81 5 77 78" in my pattern matching file?Smilie
# 5  
Old 07-23-2010
Quote:
Originally Posted by ananyob
Is there a better way to tackle this problem rather than mentioning the entire line as "81 5 77 78" in my pattern matching file?Smilie
You could, for example, include blanks and/or line endings:

Code:
[house@leonov] cat data
81 5 77 78
8373 9 7877 7878
[house@leonov] cat data | grep '77 78'
81 5 77 78
8373 9 7877 7878
[house@leonov] cat data | grep ' 77 78'
81 5 77 78
[house@leonov] cat data | grep '77 78$'
81 5 77 78
[house@leonov] cat data | grep ' 77 78$'
81 5 77 78

# 6  
Old 07-23-2010
Bug

Thanks dr.house, I just incorporated "77 78$" in my pattern matching file and it worked exactly well..Thanks IKON and dr.house for your support.......Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

2. Shell Programming and Scripting

grep with "[" and "]" and "dot" within the search string

Hello. Following recommendations for one of my threads, this is working perfectly : #!/bin/bash CNT=$( grep -c -e "some text 1" -e "some text 2" -e "some text 3" "/tmp/log_file.txt" ) Now I need a grep success for some thing like : #!/bin/bash CNT=$( grep -c -e "some text_1... (4 Replies)
Discussion started by: jcdole
4 Replies

3. Shell Programming and Scripting

Problem with "find" and "grep" command

I want to list all files/lines which except those which contain the pattern ' /proc/' OR ' /sys/' (mind the leading blank). In a first approach I coded: find / -exec ls -ld {} | grep -v ' /proc/| /sys/' \; > /tmp/list.txt But this doesn't work. I got an error (under Ubuntu): grep:... (5 Replies)
Discussion started by: pstein
5 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. Shell Programming and Scripting

ps -ef | grep "string1" "string2" " "string3"

Hi all, can any one suggest me the script to grep multiple strings from ps -ef pls correct the below script . its not working/ i want to print OK if all the below process are running in my solaris system. else i want to print NOT OK. bash-3.00$ ps -ef | grep blu lscpusr 48 42 ... (11 Replies)
Discussion started by: steve2216
11 Replies

6. AIX

xx=`date +"%a %b %d"`;rsh xxx grep "^$XX" zzz ?

AIX 4.2 I am trying to do an rsh grep to search for date records inside server logs by doing this : xx=`date +"%a %b %d"` rsh xxx grep "^$XX" zzz gives : grep: 0652-033 Cannot open Jun. grep: 0652-033 Cannot open 11. But if I do : xx=`date +"%a %b %d"` grep "^$XX" zzz it works... (2 Replies)
Discussion started by: Browser_ice
2 Replies

7. UNIX for Dummies Questions & Answers

How to use the "grep/egrep" command to search files.

Hi Team, I am new to this forum and also trying to learn Unix. I will highly appriciate your help if you can help me to get the right command . {{{ I use the command " today | egrep '(10:| 11: )' | grep ERROR " to grep all the files that has been error betweeen 10 to 11... (6 Replies)
Discussion started by: rkhanal
6 Replies

8. Shell Programming and Scripting

ls -laR | grep "^-" | awk '{print $9}'| grep "$.txt"

Hi, I don't know hot to make this command work: ls -laR | grep "^-" | awk '{print $9}'| grep "$.txt" It should return the list of file .txt It's important to search .txt at the end of the line, becouse some file name have "txt" in their name but have other extensions (13 Replies)
Discussion started by: DNAx86
13 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

10. Shell Programming and Scripting

grep to find content in between curly braces, "{" and "},"

problem String ~~~~~~~~~~~~~~~~~~ icecream= { smart peopleLink "good" LC "happy" , smartpeopleLink "dull" LC "sad" } aend = {smart vc4 eatr kalu} output needed ~~~~~~~~~~~~~~~~~~ smart peopleLink "good" LC "happy" , smartpeopleLink "dull" LC "sad" smart vc4... (4 Replies)
Discussion started by: keshav_rk
4 Replies
Login or Register to Ask a Question