mersenneforum.org  

Go Back   mersenneforum.org > Extra Stuff > Programming

Reply
 
Thread Tools
Old 2021-10-05, 18:58   #1
Plutie
 
"Evan"
Dec 2020
Montreal

1108 Posts
Default BASH scripting questions

As a part of the script I'm working on (https://www.mersenneforum.org/showthread.php?t=27185) for automating NFS polynomial optimization, for every line in a given file, I'm looking to convert a single line of space-delimited numbers into a set of lines, each with one of those numbers (discarding the last two), i.e.
Code:
120 -11281742212640 722045358858476258138115 2694891023958331982083306 -83933374113179207162110170423420169 -27085510659298648315702935344138665 51773992299728647 -130923698301412860609805316167401612 -1.26 1.024483e+25
into:
Code:
n: input_number
c5: 120
c4: -11281742212640 
c3: 722045358858476258138115 
c2: 2694891023958331982083306 
c1: -83933374113179207162110170423420169 
c0: -27085510659298648315702935344138665 
Y1: 51773992299728647 
Y0: -130923698301412860609805316167401612
What would be the best way to go about doing this? I saw some other solutions online using awk, but I don't know how I would apply that to my problem because of the different prefixes used. (https://unix.stackexchange.com/quest...umbering-order)
Plutie is offline   Reply With Quote
Old 2021-10-05, 19:25   #2
paulunderwood
 
paulunderwood's Avatar
 
Sep 2002
Database er0rr

2×7×281 Posts
Default

Code:
#!/bin/bash

while read line; do
    larray=($line)
    echo "n: input_number"
    echo "c5: ${larray[0]}"
    echo "c4: ${larray[1]}"
    echo "c3: ${larray[2]}"
    echo "c2: ${larray[3]}"
    echo "c1: ${larray[4]}"
    echo "c0: ${larray[5]}"
    echo "Y1: ${larray[6]}" 
    echo "Y0: ${larray[7]}"
done < $1
Put this in a file called, say, to_column.sh, run chmod u+x to_column.sh and use as ./to_column.sh <any_file> where any_file holds the lines.

Last fiddled with by paulunderwood on 2021-10-05 at 19:56
paulunderwood is offline   Reply With Quote
Old 2021-10-05, 20:23   #3
Plutie
 
"Evan"
Dec 2020
Montreal

23×32 Posts
Default

Quote:
Originally Posted by paulunderwood View Post
Code:
#!/bin/bash

while read line; do
    larray=($line)
    echo "n: input_number"
    echo "c5: ${larray[0]}"
    echo "c4: ${larray[1]}"
    echo "c3: ${larray[2]}"
    echo "c2: ${larray[3]}"
    echo "c1: ${larray[4]}"
    echo "c0: ${larray[5]}"
    echo "Y1: ${larray[6]}" 
    echo "Y0: ${larray[7]}"
done < $1
Put this in a file called, say, to_column.sh, run chmod u+x to_column.sh and use as ./to_column.sh <any_file> where any_file holds the lines.
Thank you! I modified it slightly to work easily with my script.

convert_polys.sh:
Code:
#!/bin/bash
# Convert msieve raw poly to CADO format
number=$2
while read line; do
    larray=($line)
    echo "n: $number" 
    echo "c5: ${larray[0]}" 
    echo "c4: ${larray[1]}" 
    echo "c3: ${larray[2]}" 
    echo "c2: ${larray[3]}" 
    echo "c1: ${larray[4]}" 
    echo "c0: ${larray[5]}" 
    echo "Y1: ${larray[6]}" 
    echo "Y0: ${larray[7]}"
    echo
done < $1
polyFinder.sh:
Code:
...
if [[ "$VERIFICATION_STRING" =~ $CADO_REGEX ]]; then
    echo File is valid sopt input.
else
    echo Invalid input, attempting to reformat.
    # pass off to separate script, convert msieve to cado format
    ./convert_polys.sh $POLYFILE $NUMBER > formatted
    POLYFILE=formatted
...
Plutie is offline   Reply With Quote
Old 2021-10-05, 20:55   #4
VBCurtis
 
VBCurtis's Avatar
 
"Curtis"
Feb 2005
Riverside, CA

11·461 Posts
Default

Note that you may wish to name this mini-script something about degree 5, since it would not work in the case where the poly lines are degree 6 (GNFS 211-ish-plus).
Or is it best to just assume the script is working with degree 5 polys only?
VBCurtis is online now   Reply With Quote
Old 2021-10-05, 20:58   #5
Plutie
 
"Evan"
Dec 2020
Montreal

23×32 Posts
Default

Quote:
Originally Posted by VBCurtis View Post
Note that you may wish to name this mini-script something about degree 5, since it would not work in the case where the poly lines are degree 6 (GNFS 211-ish-plus).
Or is it best to just assume the script is working with degree 5 polys only?
Honestly, that totally slipped my mind. I'll probably make two separate versions, called by the main script based on the degree parameter.
Plutie is offline   Reply With Quote
Old 2021-10-06, 02:19   #6
EdH
 
EdH's Avatar
 
"Ed Hall"
Dec 2009
Adirondack Mtns

22×13×79 Posts
Default

I modified my script to work all the way to degree 8, by checking the number of array elements and using if-then conditionals to add in the needed coefficients.
EdH is offline   Reply With Quote
Old 2021-10-06, 22:44   #7
Plutie
 
"Evan"
Dec 2020
Montreal

23×32 Posts
Default

another issue!

How would I reformat a set of lines given a certain order of initial characters? i.e.

Code:
 
n: 1271515007705102652189952725761191819303345301747516657201545585841235185390068713842394904588907747685724022636366668603115951556046696630803051897639628872247236774090687993328825102782693109 
Y0: -35164392518977695245121325949318512206  
Y1: 330529894414407689  
c0: -7834044196107389385481722033436772196487802910080  
c1: 32695287468220878145255421865209242060304  
c2: 166758603406144019739995422783832  
c3: -374511489403856061344417  
c4: -1148136776700228  
c5: 189189  
skew: 534376440.24
becomes:
Code:
 
n: 1271515007705102652189952725761191819303345301747516657201545585841235185390068713842394904588907747685724022636366668603115951556046696630803051897639628872247236774090687993328825102782693109
c5: 189189
c4: -1148136776700228
c3: -374511489403856061344417
c2: 166758603406144019739995422783832
c1: 32695287468220878145255421865209242060304
c0: -7834044196107389385481722033436772196487802910080
Y1: 330529894414407689
Y0: -35164392518977695245121325949318512206  
skew: 534376440.24
I'm pretty sure this is the last bit I need to have full compatibility with all (hopefully) formats of polynomials being sent here.

Last fiddled with by Plutie on 2021-10-06 at 22:45 Reason: fixed format
Plutie is offline   Reply With Quote
Old 2021-10-07, 01:18   #8
paulunderwood
 
paulunderwood's Avatar
 
Sep 2002
Database er0rr

2·7·281 Posts
Default

Code:
#!/bin/bash

readarray lines < $1
echo ${lines[0]}
i=1
j=${#lines[@]}
while ! [[ ${lines[$i]} =~ "skew" ]]; do
    i=$(expr $i + 1)
    echo ${lines[$(expr $j - $i )]}
done
echo ${lines[$i]}
HTH
paulunderwood is offline   Reply With Quote
Old 2021-10-07, 02:05   #9
Plutie
 
"Evan"
Dec 2020
Montreal

4816 Posts
Default

Quote:
Originally Posted by paulunderwood View Post
Code:
#!/bin/bash

readarray lines < $1
echo ${lines[0]}
i=1
j=${#lines[@]}
while ! [[ ${lines[$i]} =~ "skew" ]]; do
    i=$(expr $i + 1)
    echo ${lines[$(expr $j - $i )]}
done
echo ${lines[$i]}
HTH
Thank you! Works great, except that the n value becomes empty (and the actual number gets moved right above the skew value)


Code:
...
Y1: 330529894414407689 
Y0: -17582196259488608100436846735568937674 
 1271515007705102652189952725761191819303345301747516657201545585841235185390068713842394904588907747685724022636366668603115951556046696630803051897639628872247236774090687993328825102782693109
skew: 256945061.15
 
Plutie is offline   Reply With Quote
Old 2021-10-07, 02:16   #10
paulunderwood
 
paulunderwood's Avatar
 
Sep 2002
Database er0rr

2×7×281 Posts
Default

I don't understand how it loses "n:". The first line must be n and its value and not an empty line. It could be a better script, dropping "j".

Last fiddled with by paulunderwood on 2021-10-07 at 02:32
paulunderwood is offline   Reply With Quote
Old 2021-10-07, 02:54   #11
Plutie
 
"Evan"
Dec 2020
Montreal

23×32 Posts
Default

Quote:
Originally Posted by paulunderwood View Post
I don't understand how it loses "n:". The first line must be n and its value and not an empty line. It could be a better script, dropping "j".
Spotted the issue, for some reason the number for "n:" was on a new line somehow.
Plutie is offline   Reply With Quote
Reply

Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
PFGW scripting help wpolly Software 8 2019-04-12 16:47
Bash on Ubuntu on Windows henryzz Software 11 2017-07-28 21:24
Scripting startup on multiple systems stebbo Software 5 2016-09-17 08:45
Could use some scripting help (windows) Greebley Aliquot Sequences 1 2012-11-16 19:53
bash and HTTP? Dubslow Information & Answers 11 2011-12-14 21:07

All times are UTC. The time now is 03:44.


Fri Dec 3 03:44:30 UTC 2021 up 132 days, 22:13, 0 users, load averages: 1.08, 1.13, 1.21

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.