substitution

How to make sed remove lines not matched by a substitution

淺唱寂寞╮ 提交于 2019-11-27 23:04:48
问题 I basically want to do this: cat file | grep '<expression>' | sed 's/<expression>/<replacement>/g' without having to write the expression twice: cat file | sed 's/<expression>/<replacement>/g' Is there a way to tell sed not to print lines that does not match the regular expression in the substitute command? 回答1: Say you have a file which contains text you want to substitute. $ cat new.text A B If you want to change A to a then ideally we do the following - $ sed 's/A/a/' new.text a B But if

Vim - incremental numbering via regular expression search and replace

谁说胖子不能爱 提交于 2019-11-27 07:31:11
问题 I have this code: array ('id' => 1, 'name' => "Murka", 'date_of_birth' => "2014-10-31", "breed_id" => 1), array ('id' => 1, 'name' => "Jurka", 'date_of_birth' => "2014-11-31", "breed_id" => 2), array ('id' => 1, 'name' => "Nyash", 'date_of_birth' => "2014-12-31", "breed_id" => 3), array ('id' => 1, 'name' => "Meowy", 'date_of_birth' => "2014-01-31", "breed_id" => 4), array ('id' => 1, 'name' => "Yummi", 'date_of_birth' => "2014-10-31", "breed_id" => 2), array ('id' => 1, 'name' => "Barss",

Remove all newlines from inside a string

半腔热情 提交于 2019-11-27 07:25:50
I'm trying to remove all newline characters from a string. I've read up on how to do it, but it seems that I for some reason am unable to do so. Here is step by step what I am doing: string1 = "Hello \n World" string2 = string1.strip('\n') print string2 And I'm still seeing the newline character in the output. I've tried with rstrip as well, but I'm still seeing the newline. Could anyone shed some light on why I'm doing this wrong? Thanks. mipadi strip only removes characters from the beginning and end of a string. You want to use replace : str2 = str.replace("\n", "") As mentioned by @john,

Replacing Regex matches using lambda expression

那年仲夏 提交于 2019-11-27 07:17:38
问题 I'm looking for a simple regex find and replace solution were I can just provide a lambda expression for replacing each matches. E.g: regex.MatchReplace(text, match => "replacement string"); This way I can create my own logic for generating the replacement string which may involve invoking various methods etc. i.e. things you can't do with substitution patterns. Anyone know how I can accomplish this? 回答1: Regex already has one. For ex, string input="abc123def"; var output = Regex.Replace

Best way to substitute variables in plain text using PHP

╄→尐↘猪︶ㄣ 提交于 2019-11-27 06:51:22
问题 What's the best way to take some plain text (not PHP code) which contains PHP-style variables, and then substitute in the value of the variable. This is kinda hard to describe, so here's an example. // -- myFile.txt -- Mary had a little $pet. // -- parser.php -- $pet = "lamb"; // open myFile.txt and transform it such that... $newContents = "Mary had a little lamb."; I've been considering using a regex or perhaps eval() , though I'm not sure which would be easiest. This script is only going to

How to substitute text from files in git history?

巧了我就是萌 提交于 2019-11-27 06:16:32
I've always used an interface based git client (smartGit) and thus don't have much experience with the git console. However, I now face the need to substitute a string in all .txt files from history (so, not erasing the whole file but just substituting a string). I found the following command: git filter-branch --tree-filter 'git ls-files -z "*.php" |xargs -0 perl -p -i -e "s#(PASSWORD1|PASSWORD2|PASSWORD3)#xXxXxXxXxXx#g"' -- --all I tried this, and unfortunately noticed that while the password did get changed, all binary files got corrupted. Images, etc. would all be corrupted. Is there a

How to get name of variable in R (substitute)?

限于喜欢 提交于 2019-11-27 02:50:52
问题 I stacked with trying to pass variable through few functions, and on the final function I want to get the name of the original variable. But it seems like substitute function in R looked only in "local" environment, or just for one level up. Well, let me explain it by code: fun1 <- function (some_variable) {deparse(substitute(some_variable)} fun2 <- function (var_pass) { fun1 (var_pass) } my_var <- c(1,2) # I want to get 'my_var' in the end fun2 (my_var) # > "var_pass" Well, it seems like we

JavaScript - Replace all commas in a string [duplicate]

我与影子孤独终老i 提交于 2019-11-27 02:30:57
This question already has an answer here: How to replace all occurrences of a string? 56 answers I have a string with multiple commas, and the string replace method will only change the first one: var mystring = "this,is,a,test" mystring.replace(",","newchar", -1) Result : "thisnewcharis,a,test" The documentation indicates that the default replaces all, and that "-1" also indicates to replace all, but it is unsuccessful. Any thoughts? The third parameter of String.prototype.replace() function was never defined as a standard, so most browsers simply do not implement it. The best way is to use

How to use a variable as modifier in a substitution

别说谁变了你拦得住时间么 提交于 2019-11-26 22:06:23
问题 Is there a way to use a variable as modifier in a substitution? my $search = 'looking'; my $replace = '"find: $1 ="'; my $modifier = 'ee'; s/$search/$replace/$modifier; I need to use an array of hashes to make bulk search-replace with different modifiers. 回答1: Hm, if I had to do it I would do like this: use warnings; use strict; my @stuff = ( { search => "this", replace => "that", modifier => "g", }, { search => "ono", replace => "wendy", modifier => "i", } ); $_ = "this ono boo this\n"; for

bash: Bad Substitution

情到浓时终转凉″ 提交于 2019-11-26 19:37:31
#!/bin/bash jobname="job_201312161447_0003" jobname_pre=${jobname:0:16} jobname_post=${jobname:17} This bash script gives me Bad substitution error on Ubuntu. Any help will be highly appreciated. The default shell ( /bin/sh ) under Ubuntu points to dash , not bash . me@pc:~$ readlink -f $(which sh) /bin/dash So if you chmod +x your_script_file.sh and then run it with ./your_script_file.sh , or if you run it with bash your_script_file.sh , it should work fine. Running it with sh your_script_file.sh will not work because the hashbang line will be ignored and the script will be interpreted by