Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

through bash script, I am trying to find a value-number- from a text in a file, then make a new variable then replace it with a string in that file for example. in a file in /root/test.txt , i have a string web1 i need to cut the number "1", and increase it by 1 so it will be 2 then replace web1 by web2 that is what i did so far any idea how to make it works ?

m=grep 'web' /root/test.txt | awk '{print $2}'
i= $m | cut -c3
$ sed -i 's/$m/$n/g' /root/test.txt

Sample input:

 project web0

Sample output:

 project web1
share|improve this question
Please post a sample input and a sample output. – kos Feb 13 at 16:24
Input In second line in the file test.txt project web0 output: project web1 – Adam Feb 13 at 16:25
We'll need to take a look at the input file in order to be able to tell you how to extract the information you want; also personally I'm not really understanding how the output should look like, so an example would help. – kos Feb 13 at 16:29
The most fundamental issue with your approach so far is that shell variables are not expanded when inside single quotes. However you could do it much cleaner in perl since it can handle arithmetic expressions right in the replacement pattern. – steeldriver Feb 13 at 16:31
I didn't notice you edit to your comment. I've added the sample input and sample output you provided to the question. That is easily doable with Perl, but does any instance of webN have to be replaced with webN+1 or are there other constraints? – kos Feb 13 at 16:34
up vote 3 down vote accepted

AWK can search and replace text as well, so there is no need to use grep or sed. The code bellow extracts substring from second column (webN), increments N, and substitutes second field with webN+1

$ cat testInput.txt                                                                                          
project web0
project web1
$ awk '/web/{ num=substr($2,4)+1;$2="web"num };1' testInput.txt                                              
project web1
project web2

This will print edited file on screen. You can save that to another file like so awk [rest of code here] > fileName.txt and replace original with new using mv fileName.txt oldFile.txt

share|improve this answer

Using Perl:

perl -pe 's/\bweb\K[0-9]+\b/$&+1/ge' file

To edit the file in place, add the -i option:

perl -i -pe 's/\bweb\K[0-9]+\b/$&+1/ge' file
  • -p: causes Perl to assume the following loop around your program, which makes it iterate over filename arguments somewhat like sed:

         while (<>) {
             ...             # your program goes here
         } continue {
             print or die "-p destination: $!\n";
  • -e: may be used to enter one line of program.
  • s/\bweb\K[0-9]+\b/$&+1/ge: matches any web string preceded by a word boundary, discards the match and matches one or more digits followed by a word boundary, replacing the match with the equivalent number increased by 1.
% cat file
project web0
project web1
project web2
% perl -pe 's/\bweb\K[0-9]+\b/$&+1/ge' file
project web1
project web2
project web3
share|improve this answer
Beaten me by a minute :p +1 – Serg Feb 13 at 16:52

Thanks guys, I tried this code and that worked fine for me,

k=$(grep 'web' $DPATH | awk '{print $2}')      # web ends by a number#
i=$(grep 'web' $DPATH | awk '{print $2}'| cut -c3)
sed -i -e 's/'"$k"'/'"$n"'/g' $DPATH
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.