Mani
Mani

Reputation: 441

Some help needed on grep

I am trying to find alphanumeric string including these two characters "/+" with at least 30 characters in length.

I have written this code,

grep "[a-zA-Z0-9\/\+]{30,}" tmp.txt

cat tmp.txt

> array('rWmyiJgKT8sFXCmMr639U4nWxcSvVFEur9hNOOvQwF/tpYRqTk9yWV2xPFBAZwAPRVs/s
ddd73ZEjfy+airfy8DtqIqKI9+dd 6hdd7soJ9iG0sGs/ld5f2GHzockoYHfh
+pAzx/t17Crf0T/2+8+reo+MU39lqCr02sAkcC1k/LzyBvSDEtu9N/9NHicr jA3SvDqg5s44DFlaNZ/8BW37fGEf2rk13S/q68OVVyzac7IT7yE7PIL9XZ/6LsmrY
KEsAmN4i/+ym8be3wwn KWGYaIB908+7W98pI6qao3iaZB
3mh7Y/nZm52hyLa37978f+PyOCqUh0Wfx2PL3vglofi0l
QVrOM1pg+mFLEIC88B706UzL4Pss7ouEo+EsrES+/qJq9Y1e/UGvwefOWSL2TJdt

this does not work, Mainly I wanted to have minimum length of the string to be 30

Upvotes: 0

Views: 70

Answers (4)

Avinash Raj
Avinash Raj

Reputation: 174696

In the syntax of grep, the repetition braces need to be backslashed.

grep -o '[a-zA-Z0-9/+]\{30,\}' file

If you want to constrain the match to lines containing only matches to this pattern, add line-start and line-ending anchors:

grep '^[a-zA-Z0-9/+]\{30,\}$' file

The -o option in the first command line causes grep to only print the matching part, not the entire matching line.

Upvotes: 1

tripleee
tripleee

Reputation: 189317

The repetition operator is not directly supported in Basic Regular Expression syntax. Use grep -E to enable Extended Regular Expression syntax, or backslash the braces.

Upvotes: 1

fork2execve
fork2execve

Reputation: 1640

man grep

Read up about the difference between between regular and extended patterns. You need the -E option.

Upvotes: 0

Sebri Zouhaier
Sebri Zouhaier

Reputation: 745

You can use

grep -e  "^[a-zA-Z0-9/+]\{30,\}" tmp.txt

grep -e "^[a-zA-Z0-9/+]\{30,\}" tmp.txt


+pAzx/t17Crf0T/2+8+reo+MU39lqCr02sAkcC1k/LzyBvSDEtu9N/9NHicr jA3SvDqg5s44DFlaNZ/8BW37fGEf2rk13S/q68OVVyzac7IT7yE7PIL9XZ/6LsmrY
3mh7Y/nZm52hyLa37978f+PyOCqUh0Wfx2PL3vglofi0l
QVrOM1pg+mFLEIC88B706UzL4Pss7ouEo+EsrES+/qJq9Y1e/UGvwefOWSL2TJdt

Upvotes: 0

Related Questions