String increment in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String increment in UNIX
# 1  
Old 11-13-2013
String increment in UNIX

Hi,

I am able to increment numbers but unable to increment the charters in unix -AIX.

Code:
Source : AAA BB CCC
Increment Number : 5

OUTPUT:
Code:
AAA BB CCC
AAA BB CCD
AAA BB CCE
AAA BB CCF
AAA BB CCG

Thanks
onesuri
Moderator's Comments:
Mod Comment Please use CODE tags as required by the forum rules. I have made a wild guess at where the CODE tags on your input should be placed, but I have no idea if I got it right. Refusing to clearly describe your input wastes time for people in these forums trying to help you and delays any useful replies you may receive.

Last edited by Don Cragun; 11-13-2013 at 04:13 PM.. Reason: Add CODE tags.
# 2  
Old 11-13-2013
What are "the charters in unix"?

What comes after AAA BB CCZ? (AAA BB CC0, AAA BB CCa, AAA BBB CC[, AAA BB CD0, AAA BB CDa, or something else?) What characters are included in the character set for this exercise and in what order do they appear?
# 3  
Old 11-13-2013
Try this bash solution (assuming AAA BB CDA comes after AAA BB CCZ and AA comes after Z):

Code:
#!/bin/bash
S="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
function inc
{
    local I F B
    F=${1%?}
    B=${1#$F}

    case "$B" in
       (\ ) echo "$(inc "$F") " ;;
       (Z) [ -z "$F" ] && echo AA || echo "$(inc "$F")A" ;;
       (*) echo "$F${S:1+36#$B:1}"
    esac
}

read -p "Source : " src
read -p "Increment Number : " cnt

for((i=0;i<cnt;i++)) {
    echo "$src"
    src=$(inc "$src")
}


Last edited by Chubler_XL; 11-13-2013 at 05:44 PM.. Reason: Clarify assumptions made
# 4  
Old 11-13-2013
Here's another solution in bash:

Code:
#!/bin/bash
#
#
#

# check command-line for filename and
# number to increment by
if [ $# -ne 2 ]
then
    echo "Usage: ${0##*/} \"<string>\" <increment #>"
    exit 1
fi

# store the values
str=$1
i=$(expr $2 + 1 )

# loop through file and increment last character
echo $str
for((x=1;x<$i;x++))
do
    lastChar=$(echo $str | awk '{print substr($0,length($0),1)}')
    incrLetter=$(echo $lastChar | tr 'A-Y' 'B-Z')
    newStr=$(echo $str | sed "s/.$/$incrLetter/")
    echo $newStr
    str=$newStr
done

# done
exit 0

./incAlpha.sh "AAA BB CCC" 5
AAA BB CCC
AAA BB CCD
AAA BB CCE
AAA BB CCF
AAA BB CCG
AAA BB CCH

289e80450dacb315df8e7ae3eaf1ed3d
# 5  
Old 11-14-2013
Code:
#!/bin/ksh93

function incstring
{
   src=$1

   typeset -i len=${#src}
   max=$(( len - 1))

   new=""
   typeset -i dec 
   carry=0

   while ((len--))
   do
      last=${src:$len:1}
      dec=$(printf "%u" "'$last")
      (( dec != 32 )) && {
         (( len == max )) && (( dec++ ))
         (( carry == 1 )) && { (( dec++ )); carry=0; } 
         carry=0
         (( dec > 90 )) && { dec=65; carry=1; } 
      } 
      ascii=$(printf \\$(($dec/64*100+$dec%64/8*10+$dec%8)))
      new=${ascii}${new}
   done

   echo $new
}


read str?"Source: "
read cnt?"Count: " 

for ((i=0; i < cnt; i++)) {
    str=$(incstring "$str")
    echo $str
}

This User Gave Thanks to fpmurphy For This Post:
# 6  
Old 11-14-2013
@fpmurphy - Nice solution, I like the shell conversion to dec I haven't come across that before.

A slight tweak is probably required to ensure that "ZZ" increments to "AAA":

Code:
   ((carry)) && new="A$new"
   echo $new

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search partial string in a file and replace the string - UNIX

I have the below string which i need to compare with a file and replace this string in the file which matches closely. Can anyone help me on this. string(Scenario 1)- user::r--,user::ourfrd:r-- String(Scenario 2)- user::r-- File **** # file: /local/Desktop/myfile # owner: me # group:... (6 Replies)
Discussion started by: sarathy_a35
6 Replies

2. Shell Programming and Scripting

Find and increment value in string of hex

I have a long string of hex (from ASN.1 data) where I need to find and change a particular hex value only and increment it. The hex pairs either side (84 and a7) of the value to increment will remain constant. i.e. "84 <length> <value_to_increment> a7" starting with 00. So end result: ... (11 Replies)
Discussion started by: securegooner
11 Replies

3. Shell Programming and Scripting

Find and increment at each occurence of string (loop)

I created script (sh shell) to generate vlc playlist based on some data files. All works fine so far except one string I do not know how to handle with. VLCSTART='<vlc:id>' VLCV=0 VLCEND='</vlc:id>' echo -e $'\n'$'\t'$'\t'$'\t'$'\t'\$VLCSTART$VLCV$VLCENDOutput file contains several occurences... (10 Replies)
Discussion started by: TiedCone
10 Replies

4. Shell Programming and Scripting

How to increment a string variable?

Hi All, I am new to this forum and a novice at shell script. I am trying to write a script to determine each of the NIC configured on a linux system and its speed and Duplex. I came up with the following piece of code: echo `ifconfig -a | grep eth > /home/a/nic.txt` i=`awk -F, '{print... (4 Replies)
Discussion started by: pravin883
4 Replies

5. Shell Programming and Scripting

How to cut the particular string and increment the rows?

Hi am using unix aix I have tried using awk but am getting only output = x ,its not incrementing next output set -A var1 vv qa za ct=0 i=3 while do var1=`echo ${var1}` count=`awk ' NR==$i++ {print;exit}' ${.txt} | cut -c5 ` echo $count let ct=ct+1 done (6 Replies)
Discussion started by: Venkatesh1
6 Replies

6. Shell Programming and Scripting

Increment a variable in unix bash

Hello There, I have been trying to increment the value of variable to 1, 2, 3 etc. but, it displays 1 1+1 1+1+1 ..... :wall: Could anyone help out with this? for i in *.* do s=`expr $s+1` echo $s j=$i$j mv $i $j done Any help is appreciated? (24 Replies)
Discussion started by: amrutha0303
24 Replies

7. Shell Programming and Scripting

Bash shell script: Str(007) to int(7),increment it(8) & convert back to string(008)

Hi, I have the following requirement. There will be following text/line in a file (eg: search-build.txt) PRODUCT_VERSION="V:01.002.007.Build1234" I need to update the incremental build number (eg here 007) every time I give a build through script. I am able to search the string and get... (4 Replies)
Discussion started by: drwatson_droid
4 Replies

8. UNIX for Dummies Questions & Answers

increment operators in unix

increment operators in unix (3 Replies)
Discussion started by: pratima.kumari
3 Replies

9. UNIX for Dummies Questions & Answers

increment numbers in several parts of a string

I know how to do produce this: string01 string02 string03 several different ways. But how do I do produce this (without getting lost in recursion): string01morestring100yetmore10 string02morestring101yetmore20 string03morestring102yetmore30 ...... (2 Replies)
Discussion started by: uiop44
2 Replies

10. Shell Programming and Scripting

how can i increment characters in unix.. ?

hi.. i am having some 3 files : ka, kb, kc i want to use these set of files in my script... how can i do that ? like first it should process "ka" .. then "kb".. then "kc" ... thanks, Krips. (3 Replies)
Discussion started by: kripssmart
3 Replies
Login or Register to Ask a Question