Find the answer to your Linux question:
Results 1 to 6 of 6
Hi, I try to replace a string in a file like a=text b=4 c=5 I would like to replace with sed $a:$b with $a:$c I tried Code: sed 's/$a\:$b/$a\:$c/g' file.txt ...
  1. #1
    Just Joined!
    Join Date
    Mar 2010
    Posts
    4

    replace string with sed in bash

    Hi,
    I try to replace a string in a file like
    a=text
    b=4
    c=5

    I would like to replace with sed
    $a:$b with $a:$c

    I tried
    Code:
    sed 's/$a\:$b/$a\:$c/g' file.txt > file1.txt && mv file1.txt file.txt
    Any ideas how to do it in a proper way.
    Many thanks

  2. #2
    Linux Enthusiast scathefire's Avatar
    Join Date
    Jan 2010
    Location
    Western Kentucky
    Posts
    616
    you have to escape the $ too, since it has special meaning in a regexp (means the end of a string) so try:
    Code:
    sed 's/\$a\:\$b/\$a\:\$c/g'
    linux user # 503963

  3. #3
    Just Joined!
    Join Date
    Mar 2010
    Posts
    4
    Thanks,
    That didn't changed it but I found it now
    Code:
    sed "s/$a:$b/$a:$c/g"

  4. #4
    Linux Enthusiast scathefire's Avatar
    Join Date
    Jan 2010
    Location
    Western Kentucky
    Posts
    616
    are you sure you are using sed right? sed stands for Stream Editor, meaning it takes stuff from like stdout and such.

    the data has to be incoming, so you will need to cat your files you want editted, and pipe them into set
    Code:
    cat file1 | sed 's/\$a\:\$b/\$a\:\$c/g' > newfile; blah; blah etc
    so the output will be written into newfile
    linux user # 503963

  5. #5
    Just Joined!
    Join Date
    Mar 2010
    Posts
    4
    thats true,
    but thats how i did it
    Code:
    sed "s/$a:$b/$a:$c/g" file1 > newfile && mv newfile file1

  6. #6
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,096
    gnu sed knows -i, that is: inplace editing

    So, no need for that tmp file and mv.
    You must always face the curtain with a bow.

Posting Permissions

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