1

My code

#!/bin/bash
echo -n "Zadaj vetu: "
read str
echo $str | awk '{ for (i=NF; i>1; i--) printf("%s ",$i); print $1; }' | tr ' ' '\n' | tr -d '#' | tr -d '=' | tr -d '-' 

I need help, I don't understand how to make all the digits in the string that we thought to increase by one. (If there are 9 we should do 0)

example: He11o my name 3s Artem 2029 --> He22o my name 4s Artem 3130

2
  • The for loop is processing each field, not each character. Commented Apr 7, 2022 at 20:27
  • What is the tr ' ' '\n' | tr -d '#' | tr -d '=' | tr -d '-' for? Commented Apr 7, 2022 at 22:28

2 Answers 2

4

you can use the tr command for this.

echo "$str" | tr '0-9' '1-90' | tr -d '#='

Each character in the first argument is mapped to the corresponding character in the second argument. And it automatically expands ranges of characters, so this is short for tr 012456789 1234567890

Sign up to request clarification or add additional context in comments.

Comments

1

Perl to the rescue!

echo 'He11o my name 3s Artem 2029' | perl -pe 's/([0-9])/($1 + 1) % 10/ge'
  • s/PATTERN/REPLACEMNT/ is substitution;
  • the /g makes "global", it replaces all occurrences;
  • the /e interprets the replacement as code and runs it;
  • the % is the modulo operator, it will make 0 from 10.

1 Comment

thank you very much, the program has finally worked properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.