Using sed and grep/egrep to search and replace

前端 未结 6 807
小鲜肉
小鲜肉 2020-11-30 16:47

I am using egrep -R followed by a regular expression containing about 10 unions, so like: .jpg | .png | .gif etc. This works well, now I would like

相关标签:
6条回答
  • 2020-11-30 17:22

    Honestly, much as I love sed for appropriate tasks, this is definitely a task for perl -- it's truly more powerful for this kind of one-liners, especially to "write it back to where it comes from" (perl's -i switch does it for you, and optionally also lets you keep the old version around e.g. with a .bak appended, just use -i.bak instead).

    perl -i.bak -pe 's/\.jpg|\.png|\.gif/.jpg/
    

    rather than intricate work in sed (if even possible there) or awk...

    0 讨论(0)
  • 2020-11-30 17:23

    Another way to do this

    find . -name *.xml -exec sed -i "s/4.6.0-SNAPSHOT/5.0.0-SNAPSHOT/" {} \;
    

    Some help regarding the above command

    The find will do the find for you on the current directory indicated by .

    -name the name of the file in my case its pom.xml can give wild cards.

    -exec execute

    sed stream editor

    -i ignore case

    s is for substitute

    /4.6.0.../ String to be searched

    /5.0.0.../ String to be replaced

    0 讨论(0)
  • 2020-11-30 17:26

    I couldn't get any of the commands on this page to work for me: the sed solution added a newline to the end of all the files it processed, and the perl solution was unable to accept enough arguments from find. I found this solution which works perfectly:

    find . -type f -name '*.[hm]' -print0 
        | xargs -0 perl -pi -e 's/search_regex/replacement_string/g'
    

    This will recurse down the current directory tree and replace search_regex with replacement_string in any files ending in .h or .m.

    I have also used rpl for this purpose in the past.

    0 讨论(0)
  • 2020-11-30 17:29

    My use case was I wanted to replace foo:/Drive_Letter with foo:/bar/baz/xyz In my case I was able to do it with the following code. I was in the same directory location where there were bulk of files.

    find . -name "*.library" -print0 | xargs -0 sed -i '' -e 's/foo:\/Drive_Letter:/foo:\/bar\/baz\/xyz/g'
    

    hope that helped.

    UPDATE s|foo:/Drive_letter:|foo:/ba/baz/xyz|g

    0 讨论(0)
  • 2020-11-30 17:35

    try something using a for loop

     for i in `egrep -lR "YOURSEARCH" .` ; do echo  $i; sed 's/f/k/' <$i >/tmp/`basename $i`; mv /tmp/`basename $i` $i; done
    

    not pretty, but should do.

    0 讨论(0)
  • 2020-11-30 17:43

    Use this command:

    egrep -lRZ "\.jpg|\.png|\.gif" . \
        | xargs -0 -l sed -i -e 's/\.jpg\|\.gif\|\.png/.bmp/g'
    
    • egrep: find matching lines using extended regular expressions

      • -l: only list matching filenames

      • -R: search recursively through all given directories

      • -Z: use \0 as record separator

      • "\.jpg|\.png|\.gif": match one of the strings ".jpg", ".gif" or ".png"

      • .: start the search in the current directory

    • xargs: execute a command with the stdin as argument

      • -0: use \0 as record separator. This is important to match the -Z of egrep and to avoid being fooled by spaces and newlines in input filenames.

      • -l: use one line per command as parameter

    • sed: the stream editor

      • -i: replace the input file with the output without making a backup

      • -e: use the following argument as expression

      • 's/\.jpg\|\.gif\|\.png/.bmp/g': replace all occurrences of the strings ".jpg", ".gif" or ".png" with ".bmp"

    0 讨论(0)
提交回复
热议问题