LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Bash: How to increment an alphabetic value of a variable alphabetically? (https://www.linuxquestions.org/questions/linux-software-2/bash-how-to-increment-an-alphabetic-value-of-a-variable-alphabetically-4175528640/)

jdkaye 12-18-2014 02:52 AM

Bash: How to increment an alphabetic value of a variable alphabetically?
 
So for numbers it's easy.
Code:

PAGE=1; ((PAGE++)); echo $PAGE
and you get 2.

What about this?
Code:

LETTER=A; ????; echo $LETTER
And the answer is B?
jdk

bigearsbilly 12-18-2014 03:17 AM

use perl ;)

Code:

perl -e '$x=A;print $x++, "=", $x,"\n"'
A=B


TenTenths 12-18-2014 04:12 AM

If you want to do it in a loop try:

Code:

for letter in {A..Z} ; do
  echo ${letter}
done


rknichols 12-18-2014 10:41 AM

Code:

function NextLetter () {
    local Rest Letters=ABCDEFGHIJKLMNOPQRSTUVWXYZ
    Rest=${Letters#*$1}
    echo ${Rest:0:1}
}


jdkaye 12-18-2014 12:27 PM

Thanks for your efforts. Not really what I want. I have a bash script here:
Code:

#!/bin/bash
LEFT="http://www.xxx.xxx/languages-pronunciations/de/alphabetically/L/page-"
PAGE=1
URL="$LEFT$PAGE"
print $RIGHT
while wget $URL; do
((PAGE++))
URL="$LEFT$PAGE"
done

This involves getting a series of web pages containing lists of words. They are organised by page number which is why I have a loop to increment the PAGE variable to get a different page for each loop.
and so forth.
The pages are also organised by the initial letter of the word shown in bold above. So in the example above I would want to increment the "L" to "M" when all the words in "L" have been exhausted.
I hope that's clearer.
jdk

TenTenths 12-18-2014 01:41 PM

Quote:

Originally Posted by jdkaye (Post 5287000)
Thanks for your efforts. Not really what I want.

So what DO you want? Someone to write the script for you?

You asked how to go from letter to letter, you've been given that, any of those techniques could be used as an "outer loop" to go through letters with your inner loop doing the page stealing.

jdkaye 12-18-2014 02:41 PM

Quote:

Originally Posted by TenTenths (Post 5287040)
So what DO you want? Someone to write the script for you?

You asked how to go from letter to letter, you've been given that, any of those techniques could be used as an "outer loop" to go through letters with your inner loop doing the page stealing.

Obviously not. I wrote the entire script I have shown. Given the same question concerning numerical rather than alphabetic incrementing, the response would be, "Take a variable X and place it in the expression "((X++))" and its value will be incremented by one each time it's called. I hope that's clear enough for you concerning what I expect.

jdk

astrogeek 12-18-2014 03:12 PM

With a suitable replacement for LEFT, this should do what you want.

Code:

#!/bin/bash

LEFT="http://www.xxx.xxx/languages-pronunciations/de/alphabetically/"

for LETTER in A B C D E F G H I J K L M N O P Q R S T U V W X Y Z; do
PAGE=1
while wget "$LEFT$LETTER/page-$PAGE"; do
((PAGE++))
done
done

Now it does what you want...

Here is the same loop modified to only echo URL and fetch 3 pages for each letter to test it.

Code:

LEFT="http://www.xxx.xxx/languages-pronunciations/de/alphabetically/"

for LETTER in A B C D E F G H I J K L M N O P Q R S T U V W X Y Z; do
PAGE=1
while [[ PAGE -lt 4 ]]; do
URL="$LEFT$LETTER/page-$PAGE"
echo "$URL"
((PAGE++))
done
done


TenTenths 12-18-2014 03:25 PM

So with the information already in this thread and two lines added to your script to handle going from L to N, as I couldn't be bothered downloading more because 3 letters is enough to prove my letter incrementing works (and with a line to only download 10 pages max) I can download the following files:

Code:

-rwxr-xr-x 1 jdkaye jdkaye  376 Dec 18 21:16 dumbass.sh
-rw-r--r-- 1 jdkaye jdkaye 39714 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_L_page-1
-rw-r--r-- 1 jdkaye jdkaye 40534 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_L_page-10
-rw-r--r-- 1 jdkaye jdkaye 40213 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_L_page-2
-rw-r--r-- 1 jdkaye jdkaye 41005 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_L_page-3
-rw-r--r-- 1 jdkaye jdkaye 41097 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_L_page-4
-rw-r--r-- 1 jdkaye jdkaye 41041 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_L_page-5
-rw-r--r-- 1 jdkaye jdkaye 41027 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_L_page-6
-rw-r--r-- 1 jdkaye jdkaye 40427 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_L_page-7
-rw-r--r-- 1 jdkaye jdkaye 40963 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_L_page-8
-rw-r--r-- 1 jdkaye jdkaye 40396 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_L_page-9
-rw-r--r-- 1 jdkaye jdkaye 40134 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_M_page-1
-rw-r--r-- 1 jdkaye jdkaye 41424 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_M_page-10
-rw-r--r-- 1 jdkaye jdkaye 40285 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_M_page-2
-rw-r--r-- 1 jdkaye jdkaye 40515 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_M_page-3
-rw-r--r-- 1 jdkaye jdkaye 40851 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_M_page-4
-rw-r--r-- 1 jdkaye jdkaye 40983 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_M_page-5
-rw-r--r-- 1 jdkaye jdkaye 40913 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_M_page-6
-rw-r--r-- 1 jdkaye jdkaye 41041 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_M_page-7
-rw-r--r-- 1 jdkaye jdkaye 41107 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_M_page-8
-rw-r--r-- 1 jdkaye jdkaye 40440 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_M_page-9
-rw-r--r-- 1 jdkaye jdkaye 40337 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_N_page-1
-rw-r--r-- 1 jdkaye jdkaye 41055 Dec 18 21:17 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_N_page-10
-rw-r--r-- 1 jdkaye jdkaye 40466 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_N_page-2
-rw-r--r-- 1 jdkaye jdkaye 41088 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_N_page-3
-rw-r--r-- 1 jdkaye jdkaye 41548 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_N_page-4
-rw-r--r-- 1 jdkaye jdkaye 41450 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_N_page-5
-rw-r--r-- 1 jdkaye jdkaye 41212 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_N_page-6
-rw-r--r-- 1 jdkaye jdkaye 41118 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_N_page-7
-rw-r--r-- 1 jdkaye jdkaye 40908 Dec 18 21:16 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_N_page-8
-rw-r--r-- 1 jdkaye jdkaye 40969 Dec 18 21:17 ripped_from-f_r_o.com_languages-pronunciations_de_alphabetically_N_page-9

Quote:

Originally Posted by jdkaye (Post 5287075)
I hope that's clear enough for you concerning what I expect.


So yeah, I kinda get what you expect.

TenTenths 12-18-2014 03:27 PM

Quote:

Originally Posted by astrogeek (Post 5287102)
With a suitable replacement for LEFT, this should do what you want.

Code:

#!/bin/bash

LEFT="http://www.xxx.xxx/languages-pronunciations/de/alphabetically/"

for LETTER in A B C D E F G H I J K L M N O P Q R S T U V W X Y Z; do
PAGE=1
while wget "$LEFT$LETTER/page-$PAGE"; do
((PAGE++))
done
done

Now it does what you want...


Awww, you wrote it for him, how generous :) :)


Personally I used
Code:

for LETTER in {A..Z} ; do
Same result, less to type :) :)

astrogeek 12-18-2014 03:33 PM

Quote:

Originally Posted by TenTenths (Post 5287109)
Awww, you wrote it for him, how generous :) :)

Sometimes, that is just what people need.

jdkaye has been here long enough to deserve that much respect.

Quote:

Originally Posted by TenTenths (Post 5287109)
Personally I used
Code:

for LETTER in {A..Z} ; do
Same result, less to type :) :)

That is probably because you are a better bash scripter than I am! (I also used your outer loop, obviously) ;)

jdkaye 12-19-2014 01:44 AM

Thank you all very much.
(1) I wanted to use bash because beyond very simple bash scripts (I've written loads) I haven't done much bash writing.
(2) My real question was, "Does an alphabetic function exist in bash. I did not ask for nor did I expect anyone to write it for me in the event that such a function is not built into bash. I downloaded the bash manual (4.2) and did a thorough search of that manual before bothering members of this august group. I could find the numeric increment function (shown in an earlier post) and happily used it. I thought perhaps an alphabetic increment function did exist but I was too thick to find it in the manual so I asked here.
(3) I am fully aware that one can write a function doing that job but that is not what my question was and not what I expected. A simple "no, but you can write one yourself" would have sufficed.
(4) As for using Perl, one of the points of my task was teaching myself more about bash. If I wanted to do the same thing using another language I would have chosen Icon which is the language I'm most familiar with.
(5) Finally, I think I'll explore the unicode or ascii route. Now, I'll see if there's a bash function that converts octals/hexadecimals numbers to decimals and vice versa. But don't worry; I won't ask. I wouldn't want you to think I'm asking you to write my code for me.
Thank you all once again.
jdk

astrogeek 12-19-2014 02:22 AM

Focusing on post #5 I thought that your ultimate goal was to automate the download, not just to find a built-in alpha-increment feature of bash.

I did not immediately know how to best do that so thought I would learn by doing it - and I gained something by that.

I did not consider it as writing your script for you, or that you had not "done enough" on your own - who am I to make such a judgment anyway?!

My own intent was only to be helpful. Sorry if it was taken any other way, definitely not intended.

Best of luck in your explorations!

TenTenths 12-19-2014 02:26 AM

Quote:

Originally Posted by astrogeek (Post 5287116)
Sometimes, that is just what people need.

Nah, never! :)
Quote:

Originally Posted by astrogeek (Post 5287116)
jdkaye has been here long enough to deserve that much respect.

Fair point!
Quote:

Originally Posted by astrogeek (Post 5287116)
That is probably because you are a better bash scripter than I am!

Unlikely!
I just happened to have spent the last few days tidying up other peoples bash scripts including dealing with alphabet loops and date loops.

bigearsbilly 12-19-2014 02:34 AM

Quote:

Originally Posted by jdkaye (Post 5287376)
I'll see if there's a bash function that converts octals/hexadecimals numbers to decimals and vice versa.
jdk

bash builtin printf does it. ;) or dc

But, forcing shell for tasks it's not easy to do is just giving yourself a headache is it not?


All times are GMT -5. The time now is 09:03 AM.