Replace Line with Space and Backslash with a String Containing Spaces

Replace line with space and backslash with a string containing spaces

You don't need the extended regex support here (-E), POSIX-ly you could just do as below. The idea is you need to double-escape the meta-character \ to make it a literal

sed 's/--memory \(.*\) \\/--memory 100g \\/g' a.txt

or if you are sure its going to be 20g all the time, use the string directly.

sed 's/--memory 20g \\/--memory 100g \\/g' a.txt

The one advantage of using \(.*\) is that allows you to replace anything that could occur in that place. The .* is a greedy expression to match anything and in POSIX sed (Basic Regular Expressions) you need to escape the captured group as \(.*\) whereas if you do the same with the -E flag enabled (on GNU/FreeBSD sed) you could just do (.*). Also use regex anchors ^, $ if you want to match the exact line and not to let sed substitute text in places that you don't need. The same operation with ERE

sed -E 's/--memory (.*) \\/--memory 100g \\/g' file

R: How to replace space (' ') in string with a *single* backslash and space ('\ ')

Get ready for a face-palm, because this:

> gsub(" ", "\\\ ", "a b", fixed = TRUE)
[1] "a\\ b"

is actually working.

The two backslashes you see are just the R console's way of displaying a single backslash, which is escaped when printed to the screen.

To confirm the replacement with a single backslash is indeed working, try writing the output to a text file and inspect yourself:

f <- file("C:\\output.txt")
writeLines(gsub(" ", "\\", "a b", fixed = TRUE), f)
close(f)

In output.txt you should see the following:

a\b

How to replace spaces and slash in string in bash?

The following one-liner gives the desired result:

echo "$foo" | tr '\n' '\r' | sed 's,\s*\\\s*, ,g' | tr '\r' '\n'
Hello World!

we are friends

here we are

Explanation:

tr '\n' '\r' removes newlines from the input to avoid special sed behavior for newlines.

sed 's,\s*\\\s*, ,g' converts whitespaces with an embedded \ into one space.

tr '\r' '\n' puts back the unchanged newlines.

Replacing everything with a backslash till next white space

Does that work for you?

re.sub(
r"\\\w+\s*", # a backslash followed by alphanumerics and optional spacing;
'', # replace it with an empty string;
input_string # in your input string
)

>>> re.sub(r"\\\w+\s*", "", r"\fs24 hello there")
'hello there'
>>> re.sub(r"\\\w+\s*", "", "hello there")
'hello there'
>>> re.sub(r"\\\w+\s*", "", r"\fs24hello there")
'there'
>>> re.sub(r"\\\w+\s*", "", r"\fs24hello \qc23424 there")
'there'

How to replace a space with backslash and space \ so bash shell script can read the file name from a .tsv file and perform rsync copy

Using sed, one way would be to group the match and return it with a back reference appending the back slash

sed 's/\([A-Za-z0-9\/][^\.]*\) /\1\\ /g' input_file
/data-prod/bigdata/abc/test/1143-1003-004_1143-1003-905/static/common/images/header-background\ copy.jpg /mapped-data/data20/data3/header-background\ copy.jpg

How can sed replace \ (backslash + space)?

Sed recognises \ as space just fine:

bee@i20 ~ $ echo file\ 123 | sed 's/\ /+/'
file+123

Your bash script syntax is all wrong, though.
Not sure what you were trying to do with the script, but here is an example of replacing spaces with +:

ext=~/File\ with\ spaces.txt
web=`echo "$ext" | sed 's/\ /+/g'`
echo $web

Upd:
Oh, and you need the g flag to replace all occurences of space, not only the first one. Fixed above.

How to replace space with _ after last slash in a string with R

You may use negative lookahead:

gsub(" (?!.*/.*)", "_", unlist(my_list), perl = TRUE)
# [1] "abc/as 345/as_df.pdf" "adf3344/aer4_ffsd.doc"
# [3] "abc/3455/dfr.xls" "abc/3455/dfr_serf_dff.xls"
# [5] "abc/34 5 5/dfr_345_dsdf_334.pdf"

Here we match and replace all such spaces that ahead of them there are no more slashes left.



Related Topics



Leave a reply



Submit