LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   bash operations with string (https://www.linuxquestions.org/questions/linux-software-2/bash-operations-with-string-627882/)

alenD 03-13-2008 05:26 PM

bash operations with string
 
Hi all

I am looking for a bash script that would read a string
sdfsfsdf123
and output digits - 123
and the opposite, read the string and input digits in place of the current digits.

thanks in advance

matthewg42 03-13-2008 05:46 PM

What criteria should be used to identify the 123 part? Options might be:
  • The last 3 characters of the input string
  • Only digits on the end of the string
  • All after the first digit in the string
  • Any character not in the a-z range
  • All characters after position 9 in the input string
  • Some other criteria?

PatrickNew 03-13-2008 05:46 PM

To print just the digits:
Code:

echo "sdfsdfsdf123" | sed 's/[^1-9]//g'
To change the digits to some other digits, say 567
Code:

echo "sdfssdfsdf123" | sed 's/[1-9]\+/567/g'

PatrickNew 03-13-2008 05:47 PM

Quote:

Originally Posted by matthewg42 (Post 3087877)
What criteria should be used to identify the 123 part? Options might be:
  • The last 3 characters of the input string
  • Only digits on the end of the string
  • All after the first digit in the string
  • Any character not in the a-z range
  • All characters after position 9 in the input string
  • Some other criteria?

Ah, very true. I was just assuming you meant any string of numbers. That's what my code does.

konsolebox 03-13-2008 09:32 PM

thanks to your request i got fun again writing these new scripts :)

(a)
Code:

#!/bin/bash

echo -n "input new string with numbers: "
read string

# get the digits, there are many ways to do this
digits=$(echo -n "$string" | sed 's/[^1-9]//g')

# count the number of digits
dcount=$(echo -n "$digits" | wc -c)

# check digit count
if [[ dcount -eq 0 ]]; then
        echo "there where no digit found in the string"
        exit 1
fi

# print the digits
echo "digits found: $digits"

# ask for a replacement
until echo -n "please enter a $dcount digit number to replace the digits: ";
read rdigits;
[ ${#rdigits} -eq $dcount ] && [[ $rdigits =~ ^[[:digit:]]+$ ]]; do
        echo "that's not a 3 digit number!"
done

# split the strings to arrays
stringarray=( $(echo "$string" | sed 's/./& /g') )
rdigitsarray=( $(echo "$rdigits" | sed 's/./& /g') )

# print
rpos=0
for a in ${stringarray[*]}; do
        if [[ $a =~ [[:digit:]] ]]; then
                echo -n ${rdigitsarray[b++]}
        else
                echo -n ${a}
        fi
done
echo

(b)
Code:

#!/bin/bash

until

# ask for strings
echo -n "input new string with numbers: "
read string

# get the digits, there are many ways to do this
digits=$(echo -n "$string" | sed 's/[^0-9]//g')

# count the number of digits
dcount=$(echo -n "$digits" | wc -c)

# check digit count
[[ dcount -gt 0 ]]; do
        echo "there where no digit found in the string"
done

# print the digits
echo "digits found: $digits"

# ask for a replacement
until echo -n "please enter a number to replace the digits: ";
read rdigits;
[ ${#rdigits} -gt 0 ] && [[ $rdigits =~ ^[[:digit:]]+$ ]]; do
        echo "that's not a number!"
done

# split the strings to arrays
stringarray=( $(echo "$string" | sed 's/./& /g') )
rdigitsarray=( $(echo "$rdigits" | sed 's/./& /g') )

# print
rpos=0
rcount=${#rdigitsarray[@]}
spos=0
scount=${#stringarray[@]}
while [[ spos -lt scount && rpos -lt rcount && rpos -lt dcount ]]; do
        if s=${stringarray[spos++]}; [[ ${s} =~ [[:digit:]] ]]; then
                echo -n "${rdigitsarray[rpos++]}"
        else
                echo -n "${s}"
        fi
done
while [[ spos -lt scount ]]; do
        echo -n "${stringarray[spos++]}"
done
while [[ rpos -lt rcount ]]; do
        echo -n "${rdigitsarray[rpos++]}"
done
echo

The first code is strict to the actual number of digits you put in the first string

For the second one, as long as you enter a number to replace the string, it will use it.

hope this can help

regards

Edit:
btw if you want to append the extra digits of the replace string to the last digit of the first string instead of the last character, you just have to reverse the last parts of the code to:
Code:

while [[ rpos -lt rcount ]]; do
        echo -n "${rdigitsarray[rpos++]}"
done
while [[ spos -lt scount ]]; do
        echo -n "${stringarray[spos++]}"
done


alenD 03-17-2008 02:37 PM

thanx! this is very helpful...

ravi2ray 10-17-2008 02:43 PM

Thank you so much. This is very helpful. I was looking for a peace of this code
 
Thank you so much. This is very helpful. I was looking for a peace of this code


All times are GMT -5. The time now is 08:45 PM.