logo
down
shadow

Replace string between square brackets with sed


Replace string between square brackets with sed

By : Mark
Date : November 17 2020, 11:55 AM
Hope that helps 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:
code :
$ sed -i .bak 's/\[img:.*\]/\[img\]/g' file.txt
$ sed -i .bak 's/\[img:.[^]]\]/\[img\]/g' file.txt
$ sed -i 's/foo/bar/' file.txt
$ sed -i .bak 's/foo/bar/' file.txt
$ sed -i .bak 's/\[img:[^]]\{8\}\]/\[img\]/g' file.txt
# 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


Share : facebook icon twitter icon
Regex: match text square brackets and what is contained within square brackets then search and replace

Regex: match text square brackets and what is contained within square brackets then search and replace


By : Stephen Kalmakis
Date : March 29 2020, 07:55 AM
this one helps. I am trying to match (a) and replace (b) the following occurrences: , How about this?
code :
:s/\<array\[[^\]]\+\]/atoi(\0)/
Regex to replace right square brackets inside outer square brackets

Regex to replace right square brackets inside outer square brackets


By : Merit Platform
Date : March 29 2020, 07:55 AM
I wish this helpful for you No looping is necessary. We want to match every right-hand bracket but the last one and the one that immediately precedes the dot (.) separator. We simply require that our right-hand bracket is followed by a single character and specify that it must be a non-dot character. I'm not sure about the proper .NET way to say this, but you want to match on ]([^\.]) and replace with ]]$1 where $1 is a backreference to the stuff in parentheses in the match pattern. If you expect whitespace at the end of your input(s), I'd suggest cleaning that first, as this would replace the last ] if it were followed by a space.
You can test here: http://www.regular-expressions.info/javascriptexample.html
code :
~ $ echo "                              
[dim[e]nsion].[this] [member] name]
[dim[en]sion n[a]me ].[this [member] name]
[[dimension] name].[this member [name]]
" > input.txt
~ $ cat input.txt

[dim[e]nsion].[this] [member] name]
[dim[en]sion n[a]me ].[this [member] name]
[[dimension] name].[this member [name]]

~ $ sed 's/]\([^\\.]\)/]]\1/g' input.txt

[dim[e]]nsion].[this]] [member]] name]
[dim[en]]sion n[a]]me ].[this [member]] name]
[[dimension]] name].[this member [name]]]

~ $ 
Replace string only if its not followed with left square brackets i.e. [

Replace string only if its not followed with left square brackets i.e. [


By : Rick
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , [Javascript] I want to replace string only if its not followed with left hand square brackets i.e. [ , You can use negative lookahead for this:
code :
/ABC(?!\[)/
/\b\w+\b(?!\[)/
Replace all square brackets found in a string

Replace all square brackets found in a string


By : Jason Hargenrader
Date : March 29 2020, 07:55 AM
wish helps you I receive from a server some strings containing [ and ] before they reach the client side: I should convert them to ( and ). I thought I could use str_replace but I feel there should be something more "compact" (that is: if I'm not wrong, I should call str_replace on the same string four times...).
code :
<?php
$phrase  = "[10*[2+3]]+[5*2]+3";
$remov = array("[", "]");
$replc   = array("(", ")");
echo $newphrase = str_replace($remov, $replc, $phrase);
?>
How do I replace string containing square brackets

How do I replace string containing square brackets


By : ANDRODEVTURK
Date : March 29 2020, 07:55 AM
Any of those help Help me, please. There is a file with such strings: , EDIT: To look for specific string and then change line use following.
code :
awk -v old_string="source" -v new_string="result" '/<b>\[source:3:1:2\]<\/b>/{sub(old_string,new_string)} 1' Input_file
awk -v old_string="source" -v new_string="result" '/<b>\[source:3:1:2\]<\/b>/{sub(old_string,new_string)} 1' Input_file > temp && mv temp Input_file
awk 'FNR==3{sub(/source/,"result")} 1'  Input_file
awk -v old_string="source" -v new_string="result" 'FNR==3{sub(old_string,new_string)} 1' Input_file
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org