How to grep lines which have more than specific number of special characters
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
i would like to know what's the best way to grep lines from text which have more than specific number of special characters.
Let's say that you already know that each line have 4 comma ,
and you would like to grep lines which have more than 4 comma ,
Example
hi,hello,how,are,you
catch,me,then,say,hello,then
Output
catch,me,then,say,hello,then
linux awk sed regular-expression
add a comment |Â
up vote
1
down vote
favorite
i would like to know what's the best way to grep lines from text which have more than specific number of special characters.
Let's say that you already know that each line have 4 comma ,
and you would like to grep lines which have more than 4 comma ,
Example
hi,hello,how,are,you
catch,me,then,say,hello,then
Output
catch,me,then,say,hello,then
linux awk sed regular-expression
grep -E '(.*,)5' file
?
â Cyrus
Dec 22 '17 at 10:04
@Cyrus thank you and in case if you wanna grep what is less than 4 ?
â Ã±ÃÂñýàñüÃÂÃÂùcñ÷
Dec 22 '17 at 10:06
also go ahead and write your comment as answer so i gonna accept it as answer.
â Ã±ÃÂñýàñüÃÂÃÂùcñ÷
Dec 22 '17 at 10:07
grep -E '^([^,]*,),3[^,]*$' file
? This might help: The Stack Overflow Regular Expressions FAQ
â Cyrus
Dec 22 '17 at 10:19
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
i would like to know what's the best way to grep lines from text which have more than specific number of special characters.
Let's say that you already know that each line have 4 comma ,
and you would like to grep lines which have more than 4 comma ,
Example
hi,hello,how,are,you
catch,me,then,say,hello,then
Output
catch,me,then,say,hello,then
linux awk sed regular-expression
i would like to know what's the best way to grep lines from text which have more than specific number of special characters.
Let's say that you already know that each line have 4 comma ,
and you would like to grep lines which have more than 4 comma ,
Example
hi,hello,how,are,you
catch,me,then,say,hello,then
Output
catch,me,then,say,hello,then
linux awk sed regular-expression
edited Dec 22 '17 at 10:07
choroba
24.4k34067
24.4k34067
asked Dec 22 '17 at 10:00
ñÃÂñýàñüÃÂÃÂùcñ÷
407418
407418
grep -E '(.*,)5' file
?
â Cyrus
Dec 22 '17 at 10:04
@Cyrus thank you and in case if you wanna grep what is less than 4 ?
â Ã±ÃÂñýàñüÃÂÃÂùcñ÷
Dec 22 '17 at 10:06
also go ahead and write your comment as answer so i gonna accept it as answer.
â Ã±ÃÂñýàñüÃÂÃÂùcñ÷
Dec 22 '17 at 10:07
grep -E '^([^,]*,),3[^,]*$' file
? This might help: The Stack Overflow Regular Expressions FAQ
â Cyrus
Dec 22 '17 at 10:19
add a comment |Â
grep -E '(.*,)5' file
?
â Cyrus
Dec 22 '17 at 10:04
@Cyrus thank you and in case if you wanna grep what is less than 4 ?
â Ã±ÃÂñýàñüÃÂÃÂùcñ÷
Dec 22 '17 at 10:06
also go ahead and write your comment as answer so i gonna accept it as answer.
â Ã±ÃÂñýàñüÃÂÃÂùcñ÷
Dec 22 '17 at 10:07
grep -E '^([^,]*,),3[^,]*$' file
? This might help: The Stack Overflow Regular Expressions FAQ
â Cyrus
Dec 22 '17 at 10:19
grep -E '(.*,)5' file
?â Cyrus
Dec 22 '17 at 10:04
grep -E '(.*,)5' file
?â Cyrus
Dec 22 '17 at 10:04
@Cyrus thank you and in case if you wanna grep what is less than 4 ?
â Ã±ÃÂñýàñüÃÂÃÂùcñ÷
Dec 22 '17 at 10:06
@Cyrus thank you and in case if you wanna grep what is less than 4 ?
â Ã±ÃÂñýàñüÃÂÃÂùcñ÷
Dec 22 '17 at 10:06
also go ahead and write your comment as answer so i gonna accept it as answer.
â Ã±ÃÂñýàñüÃÂÃÂùcñ÷
Dec 22 '17 at 10:07
also go ahead and write your comment as answer so i gonna accept it as answer.
â Ã±ÃÂñýàñüÃÂÃÂùcñ÷
Dec 22 '17 at 10:07
grep -E '^([^,]*,),3[^,]*$' file
? This might help: The Stack Overflow Regular Expressions FAQâ Cyrus
Dec 22 '17 at 10:19
grep -E '^([^,]*,),3[^,]*$' file
? This might help: The Stack Overflow Regular Expressions FAQâ Cyrus
Dec 22 '17 at 10:19
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
3
down vote
accepted
Perl solution:
perl -ne 'print if tr/,// > 4'
-n
reads the file line by line- the tr operator returns the number of matches.
To print the lines with less than 4, just change >
to <
.
That's brilliant solution which i highly like .
â Ã±ÃÂñýàñüÃÂÃÂùcñ÷
Dec 22 '17 at 10:11
add a comment |Â
up vote
1
down vote
Using the grep
command:
grep -E '(,.*)5,' myfile
does the job. Explanation:
-E
: use an Extended Regex...
'(,.*)
: ... to find one comma followed by any number of characters, even zero...
5,'
: ... and repeat the previous pattern 5 times or more.
If you want to grep lines with less than 4 commas, replace 5,'
with 0,3'
.
add a comment |Â
up vote
1
down vote
The awk
version:
awk -F, 'NF > 5' myfile
add a comment |Â
up vote
-1
down vote
Achieved result by below one liner
l=`awk 'BEGINprint print gsub(",","")' example.txt |sed '/^$/d' |awk '$1 > 4 print NR'`;sed -n ''$l'p' example.txt
output
catch,me,then,say,hello,then
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Perl solution:
perl -ne 'print if tr/,// > 4'
-n
reads the file line by line- the tr operator returns the number of matches.
To print the lines with less than 4, just change >
to <
.
That's brilliant solution which i highly like .
â Ã±ÃÂñýàñüÃÂÃÂùcñ÷
Dec 22 '17 at 10:11
add a comment |Â
up vote
3
down vote
accepted
Perl solution:
perl -ne 'print if tr/,// > 4'
-n
reads the file line by line- the tr operator returns the number of matches.
To print the lines with less than 4, just change >
to <
.
That's brilliant solution which i highly like .
â Ã±ÃÂñýàñüÃÂÃÂùcñ÷
Dec 22 '17 at 10:11
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Perl solution:
perl -ne 'print if tr/,// > 4'
-n
reads the file line by line- the tr operator returns the number of matches.
To print the lines with less than 4, just change >
to <
.
Perl solution:
perl -ne 'print if tr/,// > 4'
-n
reads the file line by line- the tr operator returns the number of matches.
To print the lines with less than 4, just change >
to <
.
answered Dec 22 '17 at 10:09
choroba
24.4k34067
24.4k34067
That's brilliant solution which i highly like .
â Ã±ÃÂñýàñüÃÂÃÂùcñ÷
Dec 22 '17 at 10:11
add a comment |Â
That's brilliant solution which i highly like .
â Ã±ÃÂñýàñüÃÂÃÂùcñ÷
Dec 22 '17 at 10:11
That's brilliant solution which i highly like .
â Ã±ÃÂñýàñüÃÂÃÂùcñ÷
Dec 22 '17 at 10:11
That's brilliant solution which i highly like .
â Ã±ÃÂñýàñüÃÂÃÂùcñ÷
Dec 22 '17 at 10:11
add a comment |Â
up vote
1
down vote
Using the grep
command:
grep -E '(,.*)5,' myfile
does the job. Explanation:
-E
: use an Extended Regex...
'(,.*)
: ... to find one comma followed by any number of characters, even zero...
5,'
: ... and repeat the previous pattern 5 times or more.
If you want to grep lines with less than 4 commas, replace 5,'
with 0,3'
.
add a comment |Â
up vote
1
down vote
Using the grep
command:
grep -E '(,.*)5,' myfile
does the job. Explanation:
-E
: use an Extended Regex...
'(,.*)
: ... to find one comma followed by any number of characters, even zero...
5,'
: ... and repeat the previous pattern 5 times or more.
If you want to grep lines with less than 4 commas, replace 5,'
with 0,3'
.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Using the grep
command:
grep -E '(,.*)5,' myfile
does the job. Explanation:
-E
: use an Extended Regex...
'(,.*)
: ... to find one comma followed by any number of characters, even zero...
5,'
: ... and repeat the previous pattern 5 times or more.
If you want to grep lines with less than 4 commas, replace 5,'
with 0,3'
.
Using the grep
command:
grep -E '(,.*)5,' myfile
does the job. Explanation:
-E
: use an Extended Regex...
'(,.*)
: ... to find one comma followed by any number of characters, even zero...
5,'
: ... and repeat the previous pattern 5 times or more.
If you want to grep lines with less than 4 commas, replace 5,'
with 0,3'
.
edited Dec 22 '17 at 10:14
answered Dec 22 '17 at 10:08
dr01
15.3k114769
15.3k114769
add a comment |Â
add a comment |Â
up vote
1
down vote
The awk
version:
awk -F, 'NF > 5' myfile
add a comment |Â
up vote
1
down vote
The awk
version:
awk -F, 'NF > 5' myfile
add a comment |Â
up vote
1
down vote
up vote
1
down vote
The awk
version:
awk -F, 'NF > 5' myfile
The awk
version:
awk -F, 'NF > 5' myfile
answered Dec 22 '17 at 10:23
n.caillou
29216
29216
add a comment |Â
add a comment |Â
up vote
-1
down vote
Achieved result by below one liner
l=`awk 'BEGINprint print gsub(",","")' example.txt |sed '/^$/d' |awk '$1 > 4 print NR'`;sed -n ''$l'p' example.txt
output
catch,me,then,say,hello,then
add a comment |Â
up vote
-1
down vote
Achieved result by below one liner
l=`awk 'BEGINprint print gsub(",","")' example.txt |sed '/^$/d' |awk '$1 > 4 print NR'`;sed -n ''$l'p' example.txt
output
catch,me,then,say,hello,then
add a comment |Â
up vote
-1
down vote
up vote
-1
down vote
Achieved result by below one liner
l=`awk 'BEGINprint print gsub(",","")' example.txt |sed '/^$/d' |awk '$1 > 4 print NR'`;sed -n ''$l'p' example.txt
output
catch,me,then,say,hello,then
Achieved result by below one liner
l=`awk 'BEGINprint print gsub(",","")' example.txt |sed '/^$/d' |awk '$1 > 4 print NR'`;sed -n ''$l'p' example.txt
output
catch,me,then,say,hello,then
answered Dec 23 '17 at 9:30
Praveen Kumar BS
1,010128
1,010128
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f412461%2fhow-to-grep-lines-which-have-more-than-specific-number-of-special-characters%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
grep -E '(.*,)5' file
?â Cyrus
Dec 22 '17 at 10:04
@Cyrus thank you and in case if you wanna grep what is less than 4 ?
â Ã±ÃÂñýàñüÃÂÃÂùcñ÷
Dec 22 '17 at 10:06
also go ahead and write your comment as answer so i gonna accept it as answer.
â Ã±ÃÂñýàñüÃÂÃÂùcñ÷
Dec 22 '17 at 10:07
grep -E '^([^,]*,),3[^,]*$' file
? This might help: The Stack Overflow Regular Expressions FAQâ Cyrus
Dec 22 '17 at 10:19