Eliminate Hyphenated Words in Grep


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Eliminate Hyphenated Words in Grep
# 1  
Old 08-13-2012
Eliminate Hyphenated Words in Grep

How do you negate a literal hyphen/dash in a regex? If it's the first character inside the brackets, then it is read literally.
Code:
[-]

But if you stick a caret to the left of it, to negate it, then it seems it is no longer read literally. Or whatever, it doesn't work.
Code:
[^-]

Nor does escaping it seem to work.
Code:
[^\-]

I can't figure this out, and surprisingly, I couldn't come up with anything online. It seems most people are trying to accept hyphens in their regexes, but I am trying to eliminate hyphenated words from my results. No doubt I am missing something obvious and simple.
# 2  
Old 08-13-2012
Not sure what OS, shell, etc., etc., that you're using, but is this what you're trying to do?

Code:
cat file 

one
two-three
four-five
six seven
eight

grep -v '\-' file 

one
six seven
eight

This User Gave Thanks to in2nix4life For This Post:
# 3  
Old 08-13-2012
Quote:
Originally Posted by sudon't
I can't figure this out, and surprisingly, I couldn't come up with anything online.[...] No doubt I am missing something obvious and simple.
Actually this is as baffling for you as it is for me. I haven't noticed this behavior before. The GNU-sed i have at hand actually gets it right (see below).

What you can do is to "invert" the action tied to the regexp in question, for instance, using the following input file (no trailing blanks):

Code:
1 -
2
3
4
5
6 -
7
8
9
10

Code:
$ sed -n '/[-]/p' /path/to/input
1 -
6 -

would print all lines with hyphens in it. Instead of the non-working

Code:
sed -n '/[^-]/p' /path/to/input

one could use

Code:
$ sed -n '/[-]/!p' /path/to/input
2
3
4
5
7
8
9
10

which works as expected. I am aware that this is a work-around instead of a solution, but the best i can come up right now. Actually my sed got the regexp in question correct:

Code:
$ sed -n '/[^-]$/p' /path/to/input
2
3
4
5
7
8
9
10

I was first tricked by the regexp above, but it only searches for any non-hyphen and this condition is met by any line, even the ones with hyphens in them because they also have non-hyphens. When i anchored the non-hyphen to the end, i got the correct and expected result.

I hope this helps.

bakunin

Last edited by bakunin; 08-13-2012 at 08:27 PM..
# 4  
Old 08-13-2012
Quote:
Originally Posted by in2nix4life
is this what you're trying to do?

Code:
cat file 

one
two-three
four-five
six seven
eight

grep -v '\-' file 

one
six seven
eight

Yes, that is precisely what I'm trying to do. Although, I am no longer trying, I am doing. Thank you.

God, the 'v' flag.... [head shaking]....
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to grep the words with space between?

see I have a text like: 27-MAY 14:00 4 aaa 5.30 0.01 27-MAY 14:00 3 aaa 0.85 0.00 27-MAY 14:00 2 aaa 1.09 0.00 27-MAY 14:00 5 aaa 0.03 0.00 27-MAY 14:00... (3 Replies)
Discussion started by: netbanker
3 Replies

2. UNIX for Dummies Questions & Answers

Grep words with X doubles only

Hi! I'm trying to figure out how to find words with X number of doubles, only. I'm searching a dictionary, (one word per line). For instance, if you want to find words containing only one pair of double letters, you could do something like this: egrep '(.)\1' wordlist.txt |egrep -v '(.)\1.*(.)\2'... (3 Replies)
Discussion started by: sudon't
3 Replies

3. Shell Programming and Scripting

Grep multiple words with not null value

Hi, I want to grep a file if any one (GH, IJ, KL) is not null. If it is null i dont want to pull anything. cat file | awk '{print ($1)}' Parameters are : AB=123;CD=456;EF=6789; cat file | awk '{print ($2)}' GH=456;IJ=789;KL=1011 eg: Contents in file: Parameters are :... (10 Replies)
Discussion started by: Neethu
10 Replies

4. Shell Programming and Scripting

grep words from txt

Queue on node in domain description : type : local max message len : 104857600 max queue depth : 5000 queue depth max event : enabled persistent msgs : yes backout threshold : 0 msg delivery seq :... (4 Replies)
Discussion started by: Daniel Gate
4 Replies

5. Shell Programming and Scripting

grep for words in file

Hi Please can you help me on this: How to grep for multiple words in a file, BUT for every word found output it to a new line. regards FR (8 Replies)
Discussion started by: fretagi
8 Replies

6. Shell Programming and Scripting

grep words from output file

Hi, By using shell scripit i have save output in one file. I want to grep two words named CLUSTER and CLUSQMGR from that output file. How to grep that. output file would be having below words TYPE(QCLUSTER) ALTDATE(2010-05-17) CLUSTER(QS.CL.MFT1) ... (5 Replies)
Discussion started by: darling
5 Replies

7. UNIX for Dummies Questions & Answers

How to grep for two or more words in a line at the same time ...

I have a file that has multiple lines separated by an asterisk as a delimiter: FILE.txt A*123*BCD*456*TOM A*789*EFG*947*CHRIS A*840*BCD*456*TOM I would like to search multiple files for the strings 'BCD' AND 'TOM' and return the number of lines, per file, that match these two reg... (2 Replies)
Discussion started by: doza22
2 Replies

8. UNIX for Dummies Questions & Answers

Grep Three Words

I have been trying to find files containing the words AAA, BBB and CCC. I tried: grep AAA `grep BBB files*` grep CCC files* but is does not work I tried several ways this is an easy one but I am a dummy, Does anyone can help me? Thanks :( (12 Replies)
Discussion started by: murbina
12 Replies

9. Shell Programming and Scripting

find words with grep....

I have a .txt file which contains several lines of text. I need to write a script program using grep or any other unix tool so as to detect part of the text (words) between / / that begin with the symbol ~. For example if somewhere in the text appears a webpage address like... (8 Replies)
Discussion started by: chrisxgr
8 Replies

10. Shell Programming and Scripting

How to grep for two or more words in a line at the same time?

I want to search a heap of files but using an either OR or AND condition for two or more strings. How can I do this? i.e. file1 has the following file testfile primary and file2 has this ... file testfile2 secondary If I use this ... find . -type f -exec grep "testfile" {}... (2 Replies)
Discussion started by: ElCaito
2 Replies
Login or Register to Ask a Question