I have some difficulties with the sed command on Unix AIX IBM). It's seems also on linux...
here is a line of my file, that i would change :
# id=<$IdEDFRGB.RR '" fgg t uj67575 uj:$ g re ee >
it should be after the subsitution :
# id=<$Id22 02 21 17:13$ g re ee >f rgrge
when i use this sed command (after initialize the DT variable) :
sed "s/<\$Id.*\$/<\$Id${DT}\$/g"
I obtain :
# id=<$Id22 02 2021 17:41$
I loose the right part.
Someone could help me ?
thx
You need to escape the backslashes in the regexp so they're treated as literal backslash by the shell's string parser, and then they will escape the $ in the regexp. You also need to escape the $ in the regexp so that they don't start a variable expansion.
sed "s/<\\\$Id.*\\\$/<\$Id${DT}\$/g"
If you use echo ".*\$", you will see .*$, the pattern matches any text up to the string end, that's why your "right side" is eaten up. $ in a double quoted string is used to expand variables and in order to use a literal $, you need to use a "\$" char combination. In order to introduce a \$ literal text into a double quoted string, you need "\\$".
Use single quotes to avoid these issues, only use double quotes around the variable:
sed 's/\(<\$Id\).*\(\$\)/\1'"$DT"'\2/' file
Here, \(<\$Id\) is a capturing group 1 (\1) that captures <$Id literal text and \(\$\) is a capturing group 2 (\2) that captures a $ char. The $DT is within double quotes and is expanded correctly.
Using capturing groups, you avoid having to double the literal text both inside the pattern and the replacement.
Related
I'm writing strings which contain backslashes (\) to a file:
x1 = "\\str"
x2 = "\\\str"
# Error: '\s' is an unrecognized escape in character string starting "\\\s"
x2="\\\\str"
write(file = 'test', c(x1, x2))
When I open the file named test, I see this:
\str
\\str
If I want to get a string containing 5 backslashes, should I write 10 backslashes, like this?
x = "\\\\\\\\\\str"
[...] If I want to get a string containing 5 \ ,should i write 10 \ [...]
Yes, you should. To write a single \ in a string, you write it as "\\".
This is because the \ is a special character, reserved to escape the character that follows it. (Perhaps you recognize \n as newline.) It's also useful if you want to write a string containing a single ". You write it as "\"".
The reason why \\\str is invalid, is because it's interpreted as \\ (which corresponds to a single \) followed by \s, which is not valid, since "escaped s" has no meaning.
Have a read of this section about character vectors.
In essence, it says that when you enter character string literals you enclose them in a pair of quotes (" or '). Inside those quotes, you can create special characters using \ as an escape character.
For example, \n denotes new line or \" can be used to enter a " without R thinking it's the end of the string. Since \ is an escape character, you need a way to enter an actual . This is done by using \\. Escaping the escape!
Note that the doubling of backslashes is because you are entering the string at the command line and the string is first parsed by the R parser. You can enter strings in different ways, some of which don't need the doubling. For example:
> tmp <- scan(what='')
1: \\\\\str
2:
Read 1 item
> print(tmp)
[1] "\\\\\\\\\\str"
> cat(tmp, '\n')
\\\\\str
>
I'm writing strings which contain backslashes (\) to a file:
x1 = "\\str"
x2 = "\\\str"
# Error: '\s' is an unrecognized escape in character string starting "\\\s"
x2="\\\\str"
write(file = 'test', c(x1, x2))
When I open the file named test, I see this:
\str
\\str
If I want to get a string containing 5 backslashes, should I write 10 backslashes, like this?
x = "\\\\\\\\\\str"
[...] If I want to get a string containing 5 \ ,should i write 10 \ [...]
Yes, you should. To write a single \ in a string, you write it as "\\".
This is because the \ is a special character, reserved to escape the character that follows it. (Perhaps you recognize \n as newline.) It's also useful if you want to write a string containing a single ". You write it as "\"".
The reason why \\\str is invalid, is because it's interpreted as \\ (which corresponds to a single \) followed by \s, which is not valid, since "escaped s" has no meaning.
Have a read of this section about character vectors.
In essence, it says that when you enter character string literals you enclose them in a pair of quotes (" or '). Inside those quotes, you can create special characters using \ as an escape character.
For example, \n denotes new line or \" can be used to enter a " without R thinking it's the end of the string. Since \ is an escape character, you need a way to enter an actual . This is done by using \\. Escaping the escape!
Note that the doubling of backslashes is because you are entering the string at the command line and the string is first parsed by the R parser. You can enter strings in different ways, some of which don't need the doubling. For example:
> tmp <- scan(what='')
1: \\\\\str
2:
Read 1 item
> print(tmp)
[1] "\\\\\\\\\\str"
> cat(tmp, '\n')
\\\\\str
>
I am on a UNIX system that uses the Korn Shell. I'm a UNIX beginner. Here is what I want to do:
var1=user/hYbMj8d#RM1
I would like to replace only the 2nd to the last letter, which will always be an M if that matters, and change it to an O, and and store this as a new it into a new variable.
So the new variable will contain:
var2=user/hYbMj8d#RO1
Try this with sed:
sed 's/M\(.$\)/O\1/'
Full solution for storing updated var1 in var2:
var2=`echo $var1 | sed 's/M\(.$\)/O\1/'`
Explanation: using sed replace command: s/<find>/<replace>/. It searches for M.$ pattern where $ is EOL - so it will be 2nd character from the end. Then it captures remainder after M character (escaped parentheses - in sed capture groups should be escaped) and replaces it with O character and remainder after M - what was captured (it is special reference \1 - captured group #1).
How can I make sed use double quotes (") as a match?
I try to replace "source/design to "source/design_new only without changing the second coding.
Input:
"source/design",
"include/source/design",
Expect Output:
"source/design_new", #only this line renaming
"include/source/design"
I tried with command:
sed "s/\"source\/design/\"source\/design_new/g"
but it is complaining with an unmatched " error. Is there any way to use double quotes in sed?
Basically, sed takes whatever follows the s as the separator (except newline or backslash). So you can use some any other character - let's say ; - as a separator instead of /.
Example:
sed 's;"source/design;"source/design_new;g' input_file
Just use single quotes around the command instead of double quotes. This way you do not have to worry about how to handle them:
sed 's~"source/design~"source/design_new~g' file
^ ^
With your given input this command returns:
"source/design_new",
"include/source/design",
Also note you can avoid escaping every / by using another separator in sed (I used ~, although it could also be | or another one).
Welcome to tcsh. You can't include double quotes in doublequotes. End the double quotes and backslash the double quote:
sed "s/"\""source\/design/"\""source\/design_new/g"
Or, more readable (using a different separator to avoid the need to backslash slashes, and using no quotes as they're not needed for letters, slashes and underscores:
sed s=\"source/design=\"source/design_new=g
I'm writing strings which contain backslashes (\) to a file:
x1 = "\\str"
x2 = "\\\str"
# Error: '\s' is an unrecognized escape in character string starting "\\\s"
x2="\\\\str"
write(file = 'test', c(x1, x2))
When I open the file named test, I see this:
\str
\\str
If I want to get a string containing 5 backslashes, should I write 10 backslashes, like this?
x = "\\\\\\\\\\str"
[...] If I want to get a string containing 5 \ ,should i write 10 \ [...]
Yes, you should. To write a single \ in a string, you write it as "\\".
This is because the \ is a special character, reserved to escape the character that follows it. (Perhaps you recognize \n as newline.) It's also useful if you want to write a string containing a single ". You write it as "\"".
The reason why \\\str is invalid, is because it's interpreted as \\ (which corresponds to a single \) followed by \s, which is not valid, since "escaped s" has no meaning.
Have a read of this section about character vectors.
In essence, it says that when you enter character string literals you enclose them in a pair of quotes (" or '). Inside those quotes, you can create special characters using \ as an escape character.
For example, \n denotes new line or \" can be used to enter a " without R thinking it's the end of the string. Since \ is an escape character, you need a way to enter an actual . This is done by using \\. Escaping the escape!
Note that the doubling of backslashes is because you are entering the string at the command line and the string is first parsed by the R parser. You can enter strings in different ways, some of which don't need the doubling. For example:
> tmp <- scan(what='')
1: \\\\\str
2:
Read 1 item
> print(tmp)
[1] "\\\\\\\\\\str"
> cat(tmp, '\n')
\\\\\str
>