Replace string between square brackets with sed

后端 未结 1 1693
不知归路
不知归路 2020-12-07 03:46

I have some strings in a textfile that look like this:

[img:3gso40ßf]

I want to replace them to look like normal BBCode:

[i         


        
1条回答
  •  萌比男神i
    2020-12-07 04:10

    Escape those square brackets

    Square brackets are metacharacters: they have a special meaning in POSIX regular expressions. If you mean [ and ] literally, you need to escape those characters in your regexp:

    $ sed -i .bak 's/\[img:.*\]/\[img\]/g' file.txt
    

    Use [^]]* instead of .*

    Because * is greedy, .* will capture more than what you want; see Jidder's comment. To fix this, use [^]]*, which captures a sequence of characters up to (but excluding) the first ] encountered.

    $ sed -i .bak 's/\[img:.[^]]\]/\[img\]/g' file.txt
    

    Are you using an incorrect sed -i syntax?

    (Thanks to j.a. for his comment.)

    Depending on the flavour of sed that you're using, you may be allowed to use sed -i without specifying any argument, as in

    $ sed -i 's/foo/bar/' file.txt
    

    However, in other versions of sed, such as the one that ships with Mac OS X, sed -i expects a mandatory argument, as in

    $ sed -i .bak 's/foo/bar/' file.txt
    

    If you omit that extension argument (.bak, here), you'll get a syntax error. You should check out your sed's man page to figure out whether that argument is optional or mandatory.

    Match a specific number of characters

    Is there a way to tell sed that there are always 8 random characters after the colon?

    Yes, there is. If the number of characters between the colon and the closing square bracket is always the same (8, here), you can make your command more specific:

    $ sed -i .bak 's/\[img:[^]]\{8\}\]/\[img\]/g' file.txt
    

    Example

    # create some content in file.txt
    $ printf "[img:3gso40ßf]\nfoo [img:4t5457th]\n" > file.txt
    
    # inspect the file
    $ cat file.txt
    [img:3gso40ßf]
    foo [img:4t5457th]
    
    # carry out the substitutions
    $ sed -i .bak 's/\[img:[^]]\{8\}\]/\[img\]/g' file.txt
    
    # inspect the file again and make sure everything went smoothly
    $ cat file.txt
    [img]
    foo [img]
    
    # if you're happy, delete the backup that sed created
    $ rm file.txt.bak
    

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