Increase two numeric variables incrementally together


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Increase two numeric variables incrementally together
# 1  
Old 02-28-2013
Increase two numeric variables incrementally together

Hi, I was wondering if someone could help with what is probably a fairly easy problem. I have two variables, i is between 1-5, j is between 11-15

I'd like to produce this:

Code:
1_11
2_12
3_13
4_14
5_15

Each number goes up incrementally with the other.

But my shoddy code is not playing ball:

Code:
END=5
END2=15
for ((i=1;i<=END;i++))
do
for ((j=10;j<=END2;j++))
do
echo "$i"_"$j"
done
done

And is producing:

Code:
1_11
1_12
1_13
1_14
1_15
2_11
2_12
2_13
2_14
2_15
3_11
3_12
3_13
3_14
3_15
4_11
4_12
4_13
4_14
4_15
5_11
5_12
5_13
5_14
5_15

As you can see, this is not what I what I had intended Smilie

This is a small scale example - in reality i is between 1000-6000 and j is between 7000-12000 and I am creating files with this template "$i"_"$j".txt
Using this code, I am getting thousands of unwanted files. I just want $i to increase with $j, rather than every possible combination of $i and $j.

Any help using shell (rather than python/perl) scripting would be very much appreciated.
# 2  
Old 02-28-2013
Here is a bash script:
Code:
#!/bin/bash

i=1
j=11

while :
do
        if [ $i -gt 5 ] || [ $j -gt 15 ]
        then
                break
        fi

        echo "${i}_${j}"

        i=$(( i + 1 ))
        j=$(( j + 1 ))

done

This User Gave Thanks to Yoda For This Post:
# 3  
Old 02-28-2013
Smilie Brilliant. Many thanks!
# 4  
Old 02-28-2013
Code:
awk 'BEGIN  {for (i=1000; i<=6000; i++) print i"_"i+6000".txt"}'


Last edited by Jotne; 02-28-2013 at 04:50 PM..
This User Gave Thanks to Jotne For This Post:
# 5  
Old 02-28-2013
Not sure if you want to do anything else with j in your actual code, but in every case j is i+10 or (i+6000 in your full example), so you could just use:

Code:
for ((i=1;i<=END;i++))
do
    echo "$i_$(i+10)"
done

This User Gave Thanks to Chubler_XL For This Post:
# 6  
Old 02-28-2013
Hi, thanks that's also a great idea. There are instances where I could simply add on 6000, but others where I can't, so both yours and bipinajith's solutions should cover what I need. Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read files incrementally and search for particular string.

Example I have following requirements where i need to search for particular string from the log files.Files will be archived with number attached end to it and creates a new log file. First Day i will ran at 8:00 AM Filename:a.log1 Wed Aug 24 04:46:34... (1 Reply)
Discussion started by: nareshnani211
1 Replies

2. Shell Programming and Scripting

Splitting a file incrementally

Dear all, I would like to split a file incrementally. My file looks like: $path { $name "path_sparc_ifu_dec_1" ; $transition { "dtu_inst_d" v ; // (in) "U622/Y" ^ ; // (INVX16_LVT) "U870/Y" ^ ; // (AND2X1_LVT) "U873/Y" v ; // (INVX1_LVT) "U872/Y" ^ ; // (NAND3X0_LVT)... (3 Replies)
Discussion started by: jypark22
3 Replies

3. Shell Programming and Scripting

Linux command rsync sending files incrementally

Please, i have a question about rsync command: Here is the command that i have used in my script: rsync -ratlz --rsh="/usr/bin/sshpass ssh -o StrictHostKeyChecking=no" -aAXHltzh --progress --numeric-ids --devices --rsync-path="rsync" $1 $hostname:$1 using this command, i can... (0 Replies)
Discussion started by: chercheur111
0 Replies

4. UNIX for Dummies Questions & Answers

Find and Replace random numeric value with non-numeric value

Can someone tell me how to change the first column in a very large 17k line file from a random 10 digit numeric value to a non numeric value. The format of lines in the file is: 1702938475,SNU022,201004 the first 10 numbers always begin with 170 (6 Replies)
Discussion started by: Bahf1s
6 Replies

5. Programming

How to convert byteArray variables to HexaString variables for Linux?

Hello everybody, I am having problem in converting byte array variables to Hexa String variables for Linux. I have done, converting byte array variables to Hexa String variables for Windows but same function doesn't work for linux. Is there any difference in OS ? The code for Windows is given... (2 Replies)
Discussion started by: ritesh_163
2 Replies

6. Shell Programming and Scripting

How to incrementally backup data & use most recent data

Hey, dunno if the title really explains this well; basically my problem is (and this is on a router which is why the flash issue)... I have some data I want to store (it's really irelevant, i'm just counting the lines per file, basically a simple counter). I could store this data in flash but... (0 Replies)
Discussion started by: pepsi_max2k
0 Replies

7. Shell Programming and Scripting

Perl code to differentiate numeric and non-numeric input

Hi All, Is there any code in Perl which can differentiate between numeric and non-numeric input? (11 Replies)
Discussion started by: Raynon
11 Replies

8. Shell Programming and Scripting

validating variables (numeric)

Hi I need to get a user to enter a number (an exchange rate) into a script. I have the following: #!/bin/ksh echo "Enter exchange rate:" read EX_RATE if \"`" ] then echo "Well done - only numeric here" else echo "Not so well done - there is NON numeric stuff here!" fi This works... (3 Replies)
Discussion started by: pjd1
3 Replies

9. Shell Programming and Scripting

how do you to add numbers incrementally?

I've refined the filesystem size using awk and directed to a file name. eg, here's the content in a file called "numbers" $cat numbers 345 543 23423456 44435 546 . . how do you write a script to all these numbers to get the total? thanks a lot. (9 Replies)
Discussion started by: kiem
9 Replies
Login or Register to Ask a Question