Results 1 to 6 of 6

Thread: Changing a line in a text file with bash..?

  1. #1
    Join Date
    Jan 2006
    Beans
    74

    Changing a line in a text file with bash..?

    Hello

    I have a fortran program that reads an input file and then does a bunch of computations based on that file. What I want to do is to write a script to change some of the variables, say run the program 10 times and each time increment the variable input_velocity in file input.txt by 10. I know how to make all the parts except how to change that one line. I know several ways of finding it, like grep input_velocity input.txt and awk '/input_velocity/' input.txt, but that doesn't help me much..
    Could anyone give me some tips?

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Beans
    74

    Re: Changing a line in a text file with bash..?

    Heh, figured out one way to do it...
    Code:
    perl -pi -e 's/input_velocity = ".*"/input_velocity = "somethingelse"/' input.txt
    But if anyone have another way to do it, it would be great to hear from you!

  3. #3
    Join Date
    Mar 2005
    Beans
    6,040

    Re: Changing a line in a text file with bash..?

    You can also use ed and sed.
    Previously known as 23meg

  4. #4
    Join Date
    Jan 2006
    Beans
    74

    Re: Changing a line in a text file with bash..?

    Quote Originally Posted by 23meg View Post
    You can also use ed and sed.
    Yep, like this:
    Code:
    sed -i 's/input_velocity = .*/input_velocity = 200/' input.txt
    Last edited by Eric_T; July 11th, 2006 at 10:34 AM.

  5. #5
    Join Date
    Apr 2008
    Location
    Belgrade, Serbia
    Beans
    8
    Distro
    Ubuntu Studio 10.10 Maverick Meerkat

    Re: Changing a line in a text file with bash..?

    In case you need to assign to input_velocity value from variable then you might do:
    Code:
    speed=314
    replacement=s/input_velocity = .*/input_velocity = ${speed}/
    sed -i $replacement input.txt

  6. #6
    Join Date
    Dec 2007
    Beans
    1,045

    Re: Changing a line in a text file with bash..?


Bookmarks

Posting Permissions

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