How do I use grep to grab prime number output from my factor program?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How do I use grep to grab prime number output from my factor program?
# 1  
Old 11-04-2016
Question How do I use grep to grab prime number output from my factor program?

I have a factor program that runs and outputs to stdout all the prime numbers that are specified in the given paramters, in this case 30000000-31000000.

Command:
Code:
factor/factor 30000000-31000000

Sample output:
Code:
30999979 = 30999979
30999980 = 2^2 5 11 140909
30999981 = 3 10333327
30999982 = 2 13 19 62753
30999983 = 7 1663 2663
30999984 = 2^4 3 645833
30999985 = 5 29 439 487
30999986 = 2 293 52901
30999987 = 3^2 3444443
30999988 = 2^2 7749997
30999989 = 30999989
30999990 = 2 3 5 7 43 3433
30999991 = 11 2818181
30999992 = 2^3 163 23773
30999993 = 3 17 607843
30999994 = 2 15499997
30999995 = 5 13 79 6037
30999996 = 2^2 3^6 10631
30999997 = 7^3 90379
30999998 = 2 23 151 4463
30999999 = 3 10333333
31000000 = 2^6 5^6 31

How would I use a pipeline and grep to just grab the prime numbers:
Code:
factor/factor 30000000-31000000 | grep

For example Output:
Code:
30999979 = 30999979
30999989 = 30999989

# 2  
Old 11-04-2016
Hi,
Try:
Code:
factor/factor 30000000-31000000 | grep '^\([0-9]\+\)[^0-9]\+\1$'

EDIT: Another regular expression...
Code:
factor/factor 30000000-31000000 | grep -v ' .* .* '

EDIT 2: second regular expression not work in many case as: 49 = 7^2 by example Smilie
Regards.

Last edited by disedorgue; 11-04-2016 at 04:20 PM.. Reason: Add another regex for fun
# 3  
Old 11-04-2016
Or
Code:
factor/factor 30000000-31000000 | awk '$1 == $3'

# 4  
Old 11-04-2016
Or, translating RudiC's awk command to a grep command that produces the same output:
Code:
factor/factor 30000000-31000000 | grep '^\([0-9]*\) = \1$'

This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 11-06-2016
Two more options :
Code:
| grep '= [^ ]*$'

Code:
| awk NF==3

(but my favourite would be RudiC's approach)
--
Edit as disdeorgue correctly remarked the would not with for square numbers..
So adjusting for that, the first one becomes :
Code:
| grep '= [^ ^]*$'

or
Code:
| grep '= [0-9]*$'


Last edited by Scrutinizer; 11-06-2016 at 12:42 PM..
# 6  
Old 11-06-2016
Hi, Scrutinizer,
As my second solution, this 2 options not work if number is a square ( eg : 49 = 7^2 )...

Regards.
This User Gave Thanks to disedorgue For This Post:
# 7  
Old 11-06-2016
@disedorgue, you are right... I didn't stop to think how the factor program would work and only looked at the input file. The first one is still usable, with a bit of adjustment.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to output non-number lines with grep?

I want to check my data quality. I want to output the lines with non-number. I used the grep command: grep '' myfile.csv Since my file is csv file, I don't want to output the lines with comma. And I also don't want to output "." or space. But I still get the lines like the following:... (8 Replies)
Discussion started by: twotwo
8 Replies

2. UNIX for Dummies Questions & Answers

Grep SQL output file for greater than number.

Hi, This is my first post. I have a korn shell script which outputs a select statment to a file. There is only one column and one row which contains a record count of the select statement. The select statement looks something like this: SELECT COUNT(some_field) AS "count_value" ... (2 Replies)
Discussion started by: MurdocUK
2 Replies

3. Shell Programming and Scripting

Help with ahem Prime number Generating Script

Can anybody tell me why the second part of this script (Sieve of Eratosthenes) isn't working properly. This isnt coursework or homework just private studies ( Yes Project Euler began it ) I know there are easier ways of doing this too but I want to do it this way.:p Iam using Cygwin on Vista... (3 Replies)
Discussion started by: drewann
3 Replies

4. Programming

Program wont print prime numbers

The problem I'm having is that when you put in the two numbers the answer is just prime.... nothing. I cannot figure this out ive been working on this forever, can someone please god just tell me how to fix this without encrypted "hints". #include <iostream> #include <cmath> using... (3 Replies)
Discussion started by: c++newb
3 Replies

5. Shell Programming and Scripting

shell, read table and write the value for each factor to output

Hey guyz, I have a table like this: 1 A=#;B=#;C=# 2 A=#;C=#;D=#;E=#;E=# 3 B=#;B=#;B=#;D=# # are just some numbers. I want to have the output like this: * 1 2 3 A # # NA B # NA # C # # NA D NA # # E NA # NA So basically, I wanna know in each of the rows in my input (which... (9 Replies)
Discussion started by: @man
9 Replies

6. UNIX for Dummies Questions & Answers

Grab Portion of Output Text (sed, grep, awk?)

Alright, here's the deal. I'm running the following ruby script (output follows): >> /Users/name/bin/acweather.rb -z 54321 -o /Users/name/bin -c Clouds AND Sun 57/33 - Mostly sunny and cool I want to just grab the "57/33" portion, but that's it. I don't want any other portion of the line. I... (5 Replies)
Discussion started by: compulsiveguile
5 Replies

7. Shell Programming and Scripting

Perl Prime number help

Hello, I have some extra time at work and I'm trying to come up with a good prime number generator for very large numbers that can take advantage of multiple (hundreds) cores. I realize Perl may not be the best solution for this, any ideas? Thanks! (1 Reply)
Discussion started by: Kweekwom
1 Replies

8. UNIX for Dummies Questions & Answers

how to grep a number from output line

I`m having a output shown below, CFR 235,BBC DM-2 ALL CFR 111,BBC DM-2 ALL CFR 333,BBC DM-2 ALL from the above Output i want to use 235,111,333 as input for other purpose. these no always change every time i run script.so please suggest me the way i could do it with example,i have tried... (5 Replies)
Discussion started by: nitin_aaa27
5 Replies

9. Shell Programming and Scripting

Prime Number Program (Fun)

Hi, I was just wondering if anyone has, or knows where to download a prime number finder program. I would like a fairly simple bash program, and also I would like one that could take advantage of multiple processors. I have 500 cores I can use, and would like to take advantage of them using a... (2 Replies)
Discussion started by: Kweekwom
2 Replies

10. UNIX for Dummies Questions & Answers

How to grep / zgrep to output ONLY the matching filename and line number?

Hi all, I am trying to zgrep / grep list of files so that it displays only the matching filename:line number and does not display the whole line, like: (echo "1.txt";echo "2.txt") | xargs zgrep -no STRING If I use -o option, it displays the matching STRING and if not used, displays the... (3 Replies)
Discussion started by: vvaidyan
3 Replies
Login or Register to Ask a Question