Grep a word that contains minimum 5 or 6 same characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep a word that contains minimum 5 or 6 same characters
# 1  
Old 03-09-2016
Grep a word that contains minimum 5 or 6 same characters

Hi,

I am looking for a solution to grep for minimum 5 or 6 characters in a file, otherwise ignore.

Example

Code:
1121221222
2212121211
1221122122
2121222222
2222112222
1211221121

So it greps 5 X 1 or 6 X 1

Code:
2212121211
1211221121

Thanks for you help

Moderator's Comments:
Mod Comment Please use code tags for code and data

Last edited by Scrutinizer; 03-10-2016 at 03:42 AM.. Reason: typo; mod: code tags
# 2  
Old 03-09-2016
try awk:
Code:
awk 'gsub(c,c)>=5' c="1" infile

# 3  
Old 03-09-2016
Thanks for the reply.

I think I should have asked different.
I meant minimum 5 or 6 but not more than 6.

awk solution is great

Thanks

---------- Post updated at 12:23 PM ---------- Previous update was at 12:21 PM ----------

Thank,

Figured it out myself

Code:
awk 'gsub(c,c)==5' c="1"

Code:
awk 'gsub(c,c)==6' c="1"

Thanks this forum is awesome SmilieSmilieSmilieSmilieSmilieSmilieSmilie
# 4  
Old 03-09-2016
in one pass:
Code:
awk 'gsub(c,c)==5 || gsub(c,c)==6' c="1" infile

# 5  
Old 03-10-2016
And, slightly faster:
Code:
awk '(n=gsub(c,c))==5 || n==6' c="1" infile

And, if someone wants to try any of these awk suggestions on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
# 6  
Old 03-10-2016
How about
Code:
awk -F1 'NF==6;NF==7' file
2212121211
1211221121

These 2 Users Gave Thanks to RudiC For This Post:
# 7  
Old 03-10-2016
Not necessarily clearer but this should work too:
Code:
awk 'gsub(c,c)~/^[56]$/' c=1 file

or
Code:
awk -F1 'NF~/^[67]$/' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for the word and exporting 35 characters after that word using shell script

I have a file input.txt which have loads of weird characters, html tags and useful materials. I want to display 35 characters after the word "description" excluding weird characters like $&lmp and without html tags in the new file output.txt. Help me. Thanx in advance. I have attached the input... (4 Replies)
Discussion started by: sachit adhikari
4 Replies

2. Shell Programming and Scripting

Search for the word and exporting 35 characters after that word using shell script?

I have a file input.txt which have loads of weird characters, html tags and useful materials. I want to display 35 characters after the word description excluding weird characters like $$#$#@$#@***$# and without html tags in the new file output.txt. Help me. Thanx in advance. My final goal is to... (11 Replies)
Discussion started by: sachit adhikari
11 Replies

3. Shell Programming and Scripting

How ti Grep for a word and print the next word

Hi can we grep for a word and print the next word of the greped word? ex:- create or replace function function_name create function function_name we should search for word "function" and output next word "function_name" from both lines. (3 Replies)
Discussion started by: manasa_vs
3 Replies

4. Shell Programming and Scripting

grep - match files containing minimum number of pattern matches

I want to search a bunch of files and list only those containing a minimum number of pattern matches. So if I want to identify files containing 3 (or more) instances of the pattern "said:" and I have file1 that contains the lines: He said: She said: and file2 that contains the lines: He... (3 Replies)
Discussion started by: stumpyuk
3 Replies

5. Shell Programming and Scripting

grep part of word or Another word from a string

Hi all, FileOne family balance >>>>> 0 0 0 0 java.io.FileNotFoundException: Settings.xml (No such file or directory) at java.io.FileInputStream.open(Native Method) .. .... ..... ..... java.lang.NullPointerException ... ..... ...... Stacktrace: at... (2 Replies)
Discussion started by: linuxadmin
2 Replies

6. Shell Programming and Scripting

How to grep a word and next column to that word?

Hi, I have input file as below. Can you help me? inac_4y;0;2;Balance;200;1;1; 0;2;Balance;100;1; 0;inac_nq;0;1;Balance;100;1 desired output Balance;200 Balance;100 Balance;100 -Suresh Please use and tags when posting code, data or logs etc. to preserve formatting... (5 Replies)
Discussion started by: suresh3566
5 Replies

7. Shell Programming and Scripting

Grep out specific word and only that word

ok, so this is proving to be kind of difficult even though it should not be. say for instance I want to grep out ONLY the word fkafal from the below output, how do I do it? echo ajfjf fjfjf iafjga fkafal foeref afoafahfia | grep -w "fkafal" If i run the above command, i get back all the... (4 Replies)
Discussion started by: SkySmart
4 Replies

8. Shell Programming and Scripting

remove strings of lowercase characters (with minimum length requirement)

Hi all, I want to delete all lowercase characters from my file, but only strings of length 7 and more. For example, how can I go from: JHGEFigeIGDUIirfyfiyhgfoiyfKJHGuioyrDHG To: JHGEFigeIGDUIKJHGuioyrDHG There should be a trick to add to sed 's///g', but I can't figure it out.... (2 Replies)
Discussion started by: elbuzzo
2 Replies

9. UNIX for Dummies Questions & Answers

how to grep the word and display only the second word from it

hi, consider the below line in a text file, 'Y',getdate(),'N','V',NULL ..... 'N',getdate(),'Y','D',NULL ..... 'Y','N','Y',getdate(),'Y','D',NULL .... as u see above, i want only the second word after the getdate() word... getdate() will not come 2nd word alwys it may be any position but i... (11 Replies)
Discussion started by: prsam
11 Replies

10. UNIX for Dummies Questions & Answers

how to grep for a word and display only the word

Hi, When we "grep" for a word in a file, it returns the lines containing the word that we searched for. Is there a way to display only the words and not the entire line containing them. Thanks Ananth (6 Replies)
Discussion started by: ananthmm
6 Replies
Login or Register to Ask a Question