Find the answer to your Linux question:
Results 1 to 6 of 6
Hello, I want to replace group of words in a text file. Text file content: set val(xyz) 50; set val(abc) 10; configure xyz $val(xyz) \ configure ixyz $val(abc) \ I ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jan 2014
    Location
    Gujarat, India
    Posts
    3

    Replacing words in a text file using shell script.


    Hello,

    I want to replace group of words in a text file.

    Text file content:


    set val(xyz) 50;
    set val(abc) 10;
    configure xyz $val(xyz) \
    configure ixyz $val(abc) \

    I WANT TO REPLACE "val(xyz) 50" by "val(xyz) 25" than "val(xyz) 75" and lastly "val(xyz) 100" and performing specific logic after every replacement.

    I HAVE USED FOR LOOP as mentioned below:

    for i in 25 50 75 100;
    do
    perl -pi -e "s/val\(xyz\) \K50/$i/g" /home/guest/Desktop/file.txt
    #further logic simulation here
    perl -pi -e "s/val\(xyz\) \K$i/50/g" /home/guest/Desktop/file.txt
    done

    have saved this above loop in file named test.sh
    i am executing this filem by terminal by sh test.sh command.
    BUT THE OUTPUT IS NOT DESIRED ONE.

    While execution of first "for loop" the o/p is perfect.
    OUTPUT IS

    set val(xyz) 25;
    set val(abc) 10;
    configure xyz $val(xyz) \
    configure ixyz $val(abc) \

    BUT FOR THE 2ND TIME THE OUTPUT CHANGES

    set val(xyz) 2525;
    set val(abc) 10;
    configure xyz $val(xyz) 25\
    configure ixyz $val(abc) \

    WHAT IS THE SOLUTION?

  2. #2
    Linux Engineer
    Join Date
    Dec 2013
    Posts
    1,188
    Are you using bash to execute the script? It runs as expected when I try. bash 4.2.45 perl 5.14

  3. #3
    Just Joined!
    Join Date
    Jan 2008
    Posts
    1
    I like using sed to "s"ubstitute one string for another. I have a sed line to get rid of the ?= and here is what works for me:

    sed 's/^To: Michael?= /To: Michael /g'

    This line is several lears old, and I think the enclosing tics may or may not be needed now. The / character is a delimiter. The ^ means beginning of line which ^ you probably want to delete. The ^To: Michael?= which includes a trailing space is the search for string. The To: Michael is the replace string. I forget what the g is for.

    All the Best!

  4. #4
    Just Joined!
    Join Date
    Jan 2014
    Location
    Gujarat, India
    Posts
    3
    Hello gregm, Thank you for the quick reply.
    Ya I amusing bash.

  5. #5
    Just Joined!
    Join Date
    Jan 2014
    Location
    Gujarat, India
    Posts
    3
    Hello mikecolley, Thank you for the quick reply.
    have place sed -i sed 's/^To: Michael?= /To: Michael /g' /home/guest/desktop/file.txt

    none of the text is replaced in the specified text file

  6. #6
    Linux Engineer
    Join Date
    Dec 2013
    Posts
    1,188
    Quote Originally Posted by bhargavmakodia View Post
    Hello gregm, Thank you for the quick reply.
    Ya I amusing bash.
    To further elaborate: the loop does what is expected when I run it. Here is what I tested with. If you run it and see something different then it may be a perl version issue. If you see the same thing but something different from your script then post the actual script that is behaving differently.

    test.sh:
    Code:
    !/bin/bash
    
    for i in 25 50 75 100;
    do
            perl -pi -e "s/val\(xyz\) \K50/$i/g" test.txt
            cat test.txt
            perl -pi -e "s/val\(xyz\) \K$i/50/g" test.txt
            cat test.txt
    done
    test.txt:
    Code:
    set val(xyz) 50;
    set val(abc) 10;
    configure xyz $val(xyz) \
    configure ixyz $val(abc) \

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •