Find the answer to your Linux question:
Results 1 to 10 of 10
How can you find and replace text in a file via CLI?...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Engineer
    Join Date
    Jan 2005
    Location
    Chicago (USA)
    Posts
    1,028

    CLI find and replace


    How can you find and replace text in a file via CLI?

  2. #2
    Linux Guru dylunio's Avatar
    Join Date
    Aug 2004
    Location
    Cymru
    Posts
    4,157
    I think you should be able to use the find as well as the sed commands, but I'm not sure of the syntax, so check their man pages.
    Registered Linux User #371543!
    Get force-get May The Source Be With You
    /dev/null
    /dev/null2

  3. #3
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by dylunio
    I think you should be able to use the find as well as the sed commands, but I'm not sure of the syntax, so check their man pages.
    Er, sed does the finding before it replaces! find has its uses, but not for finding and replace text in a file (finding the files to operate on, that's another matter).

  4. #4
    Linux Engineer
    Join Date
    Jan 2005
    Location
    Chicago (USA)
    Posts
    1,028
    How would I find a regular expression ("[\d][\d]\)") and replace it with "*"?
    sed -re "s/[\d][\d]\)/*/g" wiki
    and
    sed -re "s/^[\d][\d]\)/*/g" wiki
    don't work. Nothing changes. I know the regular expression is right because I've tried it in Kate. I don't want to use Kate for everything because that would take a long time.

  5. #5
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Now wait. Are you trying to change every place that [\d][\d]\) matches, or are you trying to replace every literal instance of [\d][\d]\)?

    For example, do you want to replace:

    a) "11)"

    or

    b) "[\d][\d]\)"

    ?


    If a), what you have should work. If b), you'll probably need:
    Code:
    sed -re "s/\[d\]\[d\]\)/*/g" wiki
    I haven't tested that, though.

  6. #6
    Linux Engineer
    Join Date
    Jan 2005
    Location
    Chicago (USA)
    Posts
    1,028
    That didn't do anything.
    I want to replace 10), 11), 12) et cetera to make a WikiMedia wiki cleaner.

  7. #7
    Linux Engineer
    Join Date
    Sep 2003
    Location
    Knoxhell, TN
    Posts
    1,078
    to use a literal asterisk in sed, you need \* not just *... * has special meaning in regex syntax (0 or more occurrences of the previous character)..
    Their code will be beautiful, even if their desks are buried in 3 feet of crap. - esr

  8. #8
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Okay, I just got it to work:

    Code:
    sed -re "s/^[0-9][0-9]\)/*/g" wiki
    I've found that sometimes \d doesn't work very well, so you have to manually type in [0-9].

    Also, that worked on my test file. Lemme know if it doesn't work for you.


    EDIT:

    And lordnothing, in s/FOO/BAR, BAR is interpreted as a literal string. So you can use an asterisk there.

    EDIT 2:

    That command actually only prints it and doesn't replace the file.

    To actually replace the file, I did:
    Code:
    sed -re "s/^[0-9][0-9]\)/*/g" wiki -i
    The "-i" will edit in place instead of outputting it to the CLI.

    If you want to backup the file, you could do:
    Code:
    sed -re "s/^[0-9][0-9]\)/*/g" wiki -ibak
    Which would copy wiki to wiki.bak, then edit wiki.

  9. #9
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Also, you don't need to escape the ) in pattern1. In fact you shouldn't because it has special meaning in sed when used in conjunction with \(.
    Code:
    echo "fred=bert" | sed 's/\([^=]*\)=\(.*\)/\2=\1/'
    produces the result "bert=fred".

  10. #10
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by lordnothing
    to use a literal asterisk in sed, you need \* not just *... * has special meaning in regex syntax (0 or more occurrences of the previous character)..
    Only in pattern1, not in the replacement string.

Posting Permissions

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