Find the answer to your Linux question:
Results 1 to 10 of 10
I have done some searching, seeing what commands I exactly will need. I/O redirection grep I think echo and a while loop? What I want to do is lets say ...
  1. #1
    Just Joined!
    Join Date
    Nov 2007
    Posts
    14

    Finding and replacing a line in a file

    I have done some searching, seeing what commands I exactly will need.
    I/O redirection
    grep I think
    echo
    and a while loop?

    What I want to do is lets say I have a file with 1000 lines of configuration settings. I want to search the file for a specific line ( I know what the line is ) and once I find this line, I want to replace the line with a new line of code that I give it.

    How can this be achieved with linux bash/shell scripting? Do I need something more powerful like perl/python for this task? I would like to avoid those dependencies at all costs, even if it makes it inefficient.

    Thanks,

    Michael

  2. #2
    Just Joined!
    Join Date
    Jan 2008
    Posts
    25
    The simplest solution is to use 'sed' like this:
    sed -e 's/{line that you want to replace}/{line with which you want to replace}/g'

    of course, if your line has special character's then you need to use proper sed syntax which you can easily find by google it or for eg. from here: Sed - An Introduction and Tutorial

    There are of course many other ways to do it like using awk, etc.

  3. #3
    Just Joined!
    Join Date
    Nov 2007
    Posts
    14
    If this 'sed' command doesn't require dependencies (works on most common linux distros) then I'll use it.

    My original plan was to use the 'cat' command in a while loop and the 'while read line' and then check Each line that came in, and if the line matched then I would change it. I'll try the sed when I get home!

    Thanks.

  4. #4
    Just Joined!
    Join Date
    Jan 2008
    Posts
    25
    It shouldn't. I would be surprised if any distros comes without sed.

  5. #5
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    They may not all support the -e (edit in-situ) flag, though, in which case you'd have to use a temporary intermediate file.

  6. #6
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Quote Originally Posted by scm View Post
    They may not all support the -e (edit in-situ) flag, though, in which case you'd have to use a temporary intermediate file
    I suppose you mean the -i flag?

    Regards

  7. #7
    Just Joined!
    Join Date
    Jan 2008
    Posts
    25
    Quote Originally Posted by Franklin52 View Post
    I suppose you mean the -i flag?

    Regards
    No! scm actually meant -e flag. Using this flag one can chain many sed commands in a single line.

  8. #8
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    No, I did mean the -i flag that the posting didn't actually contain - the example given requires the use of an intermediate file. I should really read posts properly before I rush in.

  9. #9
    Just Joined!
    Join Date
    Jan 2008
    Posts
    25
    Quote Originally Posted by scm View Post
    No, I did mean the -i flag that the posting didn't actually contain ?
    Aah, right! forgot about this. Anyway, -i option works for me in Red hat/Fedora, ubuntu and Scietific L.

  10. #10
    Just Joined!
    Join Date
    Nov 2007
    Posts
    14
    Hrm...

    Lets say my file has a line that can look like this:

    My Configuration = [ int, int, int, int ]

    OR

    My Configuration = [ int, int]

    Basically there can be any number of integers within the two brackets, but at least one occurrance.

    would my sed statement look like:

    sed 's/"My Configuration = "\[{int}{",int"}*\]' <mysettings.txt >mysettings.txt

    so I'm looking for the My Config. with an [ bracket, then Definitely 1 occurrence of int, with zero or more occurance of ,int after.

    Does that make sense?

Posting Permissions

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