Substituting string excerpts via sed, awk and other utilities.
Questions tagged [string-substitution]
220 questions
2
votes
2 answers
Insert dots around substring using gsub in R
I would like to have a function in R that inserts dots (".") around a given substring (e.g. "alpha") if they are not already present. E.g.
string="10-alpha-epoxy-Amorph-4-ene" should return
"10-.alpha.-epoxy-Amorph-4-ene"
string="alpha-cadolene"…

Tom Wenseleers
- 7,535
- 7
- 63
- 103
2
votes
2 answers
Rename files and directories using substitution and variables
I have found several similar questions that have solutions, except they don't involve variables.
I have a particular pattern in a tree of files and directories - the pattern is the word TEMPLATE. I want a script file to rename all of the files and…

rednectar
- 101
- 2
- 12
2
votes
2 answers
Replace a substring in Lua with a pattern
I have a string like this
str = '["username"] = "user";
["deepscan"] = "true";
["token"] = true;
["password"] = "krghfkghkfghf";
["uploadMethod"] = "JSON";
["serviceIsRunning"] = {};
["host"] = "sample.com";
…

Joseph Philbert
- 635
- 7
- 8
2
votes
2 answers
Porting Perl string substitution to Ruby?
I'm trying to figure out how to do string substitutions while in the process of porting a Perl script to Ruby.
Here's the Perl line. I'm trying to figure out Ruby's equivalent:
$historyURL =~ s/COMPONENT_NAME/$componentName/g;
For those of you who…

dsw88
- 4,400
- 8
- 37
- 50
2
votes
1 answer
How to make this Java (Android) String substitution?
I don't understande why this is not working:
(method is taken from HERE on SO).
private String MakeSizeHumanReadable(int bytes, boolean si) {
int unit = si ? 1000 : 1024;
if (bytes < unit) return bytes + " B";
int exp = (int)…

dentex
- 3,223
- 4
- 28
- 47
2
votes
4 answers
Python regex to remove whitespace inside a pattern match
I have some well-behaved xml files I want to reformat (NOT PARSE!) using regex. The goal is to have every pairs as oneliners.
The following code works, but I'd like to get the operations performed in a single regex substitution instead of…

heltonbiker
- 26,657
- 28
- 137
- 252
2
votes
2 answers
Skipping particular positions in a string using substitution operator in perl
Yesterday, I got stuck in a perl script. Let me simplify it, suppose there is a string (say ABCDEABCDEABCDEPABCDEABCDEPABCDEABCD), first I've to break it at every position where "E" comes, and secondly, break it specifically where the user wants to…

prashant
- 97
- 1
- 1
- 8
1
vote
2 answers
Regex to replace substring occurrences *only* when they are enclosed between begin / end brackets
regex problem in ruby, and for some reason, I need this in one line in gsub method of ruby
Suppose the input sample string variable is multi line string like below
begin1 item abc item abc item
extra end1
begin2 item abc item abc extra end2
begin1…

Larry Cai
- 55,923
- 34
- 110
- 156
1
vote
1 answer
Why do I get different results in perl from the s, y, and tr operators?
perl -pe 's/A/ā/' <<< "avatAr" gives me avatār as expected.
But both perl -pe 'y/A/ā/' <<< "avatAr" and perl -pe 'tr/A/ā/' <<< "avatAr" give me avat�r.
Why is this so? And how might I get the same results as the substitution?
I also tried perl…

chandra
- 292
- 2
- 11
1
vote
1 answer
Character joining and deleting in R
I have a data and it has a column yearmonthweek. For example, 2012y05m1wk means 1st week of May, 2012. Likewise, 2012y11m3wk means 3rd week of November, 2012.
data looks like…

Lee922
- 77
- 3
1
vote
0 answers
Regex substitution reversal?
I have a question:
starting from this text example:
input_test = "أكتب الدر_س و إحفضه ثم إقرأ القصـــــــــــــــيـــــــــــدة"
I managed to clean this text using these functions:
arabic_punctuations =…

mohanad almowahid
- 17
- 3
1
vote
3 answers
Multiple patterns and replacements from "dictionary" dataframe in R, maybe with gsub?
I have a dataset with strings (data$text) containing names of emojis instead of actual images (e.g., FACE_WITH_TEARS_OF_JOY). Now I'm trying to replace each emoji name with the actual emoji. The names and emojis are saved in an extra dataset which…

just_asking
- 13
- 3
1
vote
2 answers
Rails 3: how to use gsub or to replace whitespace characters with "-"?
I have an Artist model is name:string. and I want /users/1/artists/jimi-hendrix/posts instead of what I have now which is /users/1/artists/1/posts
The problem is I don't think I can use friendly_id for the artist's name, this is because I have…

trying_hal9000
- 4,343
- 8
- 43
- 62
1
vote
2 answers
Append specific caracter at the end of each line
I have a file and I want to append a specific text, \0A, to the end of each of its lines.
I used this command,
sed -i s/$/\0A/ file.txt
but that didn't work with backslash \0A.

lystack
- 41
- 4
1
vote
2 answers
Remove comma from substring in Python
The string is the following:
s = 'AUDC,AUDIOCODES COM,+55,27.49,26.47,"$1,455.85",($56.10),($56.10),-3.71%'
I would like the comma inside this substring "$1,455.85" to be removed but not the other commas.
I tried this but failed:
import re
pattern…

user8270077
- 4,621
- 17
- 75
- 140