LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-07-2012, 03:17 PM   #1
dwezel
LQ Newbie
 
Registered: Sep 2005
Posts: 12

Rep: Reputation: 0
grep for words with special characters


I am looking for some help with a grep statement that will let me pull words out of a file that are between 8-15 alpha numeric characters including capitals but also words that have !@#$%^& in them in place of characters as in p@ssword or dollar$.

A couple things I have tried are:

grep -o '\<[a-zA-Z0-9]\{8,15\}[^a-zA-Z]*\>'
grep -o \<*\{8,15\}\>

but no luck. TYIA
 
Old 07-07-2012, 03:59 PM   #2
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 593

Rep: Reputation: 140Reputation: 140
If you use the -E option you don't have to use \
the | symbol means or so grep searches for 8 or 9 or 10,11,12,13,14 or 15
Code:
bash-4.2# echo {1..20} | grep -oE "[89]|[1][0-5]" 
8
9
10
11
12
13
14
15
8
9
If you only want whole words so not the 8 in 28 you can use the -w option
Code:
bash-4.2# echo {1..20} | grep -owE "[89]|[1][0-5]" 
8
9
10
11
12
13
14
15
 
Old 07-07-2012, 04:07 PM   #3
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 593

Rep: Reputation: 140Reputation: 140
Use double quotes for the special characters and you have to escape !
Code:
bash-4.2# echo \!@#$%^\& | grep -o "\!@#$%^&"
!@#$%^&
 
Old 07-07-2012, 04:08 PM   #4
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Have you tried simply:
Code:
$ grep -o '[a-z!@#$%&\^]\{8,15\}' bigfile.txt
??

I have not thoroughly tested that. Make sure it's matching a large number of test cases before trusting it.

---

I was too slow.
 
1 members found this post helpful.
Old 07-07-2012, 04:14 PM   #5
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 593

Rep: Reputation: 140Reputation: 140
With xargs you can get rid of the newlines.
Code:
bash-4.2# echo {1..20} | grep -owE "[89]|[1][0-5]" | xargs
8 9 10 11 12 13 14 15
 
Old 07-07-2012, 04:23 PM   #6
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 593

Rep: Reputation: 140Reputation: 140
If you use a set you can omit the escape but you can't use ^ as first character in the set without escaping it
Code:
bash-4.2# echo "\!@#$%^&" | grep -o "[!@#$%^&]" | xargs
! @ # $ % ^ &
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] GREP: search special characters carolflb Linux - Newbie 4 02-10-2010 05:11 AM
Command to delete words less than 5 characters? Dazamondo Linux - Newbie 3 07-24-2009 03:28 PM
How do I extract characters from several words on a line? MheAd Linux - Newbie 22 06-23-2008 11:22 PM
VIM: How to delete all lines that include special words. cryincold Linux - Software 3 02-10-2008 12:17 AM
grep problem in extracting special characters imppayel Programming 8 12-02-2004 08:28 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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