How to escape any backslash inside a file using sed - bash

Hi I have a file which has a string in it that is in an array format and i'm using jq to loop through each index and have some logic depends on the string on each index. However there is a backslash character and jq returns invalid escape character. So I think I need to escape the backlash. How can I do that using sed?
Example:
["*","*Cases","*/opt/selenium/tests/src/test/cases","*../../src/test/cases","*01 Login","*/opt/selenium/tests/src/test/cases/01_login.robot","*../../src/test/cases/01_login.robot","*TC001 Verify the login","*<p>If member has clicked 'Remember Me' check box, after the deployment, it's not necessary to input account and password when login. Web do not have the 'Remember Me' function\x3c/p>","*Login button is not visible after 60 seconds"]

You wrote you want to escape any backslash (\), not slash (/). Assuming that the escape character is backslash itself, then the following should do the job:
sed -i 's/\\/\\\\/g' filename
This can become even shorter:
sed -i 's/\\/&&/g' filename
but using literals in the replacement instead of the variable & is more efficient.
In case you just made a kind of typo, and you want instead to escape slashes with backslashes, then the following command should suffice:
sed -i 's|/|\\/|g' filename
(Since I used | instead of /, I don't need to escape the / in the replacement.)

You can escape / with \/ in sed like this:
sed -i "s#\/#\\\/#g" filename

The error is probably caused by the escape sequence \x3c appearing in one of the string literals.
That is a valid escape sequence in JavaScript (it represents the character <), but in JSON it is invalid.
JSON does support Unicode escapes, though: \u003c.
Apparently you got your input from some kind of JavaScript serializer.
The best solution would of course be to replace that by a proper JSON serializer.
If that is not possible or not practical (for example because it comes from a third party),
then you could use sed to replace every \xnn with its Unicode counterpart \u00nn.
sed 's/\\x\([0-9A-Fa-f]\{2\}\)/\\u00\1/g' poorjson.txt
Output:
["*","*Cases","*/opt/selenium/tests/src/test/cases","*../../src/test/cases","*01 Login","*/opt/selenium/tests/src/test/cases/01_login.robot","*../../src/test/cases/01_login.robot","*TC001 Verify the login","*<p>If member has clicked 'Remember Me' check box, after the deployment, it's not necessary to input account and password when login. Web do not have the 'Remember Me' function\u003c/p>","*Login button is not visible after 60 seconds"]
Please note that this is not 100% foolproof.
It does not check if the backslash itself has not been escaped.

Related

Special characters in script not working correctly

I am trying to write a script to change a password on multiple servers. The issue is that the password has special characters. This is the example of the echo command I am using:
echo -e "'P#7g$dkW$8Ej$5$%'\n'P#7g$dkW$8Ej$5$%'"
Here is the response:
[root#myserver ~]# echo -e "'P#7g$dkW$8Ej$5$%'\n'P#7g$dkW$8Ej$5$%'"
'P#7gEj$%'
'P#7gEj$%'
As you can see it is dropping a lot of characters. Any advice would be helpful.
Take out the double quotes and just use single quotes so variables (preceded by $) aren't expanded.
The reason why your string is interpreted that way is because you have used double-quotes to create the string. The single quote that you have set after that will be interpreted as a part of the string and has no special meaning anymore and doesn't make your password a literal string. That means that the password string is still in the scope of "" and $xyz will be interpreted as the xyz variable.
You can try to concatenate them like this
passwd1='password1'
passwd2='password2'
echo -e "$passwd1\n$passwd2"
or just leave out the double quotes in your solution.

sed / Batch / Windows: Prevent changig Backslash to slash

I have a variable with a path, like this:
SET "somevar=D:\tree\path\nonsens\oink.txt"
And I have a file, where somethink like this is written
VAR=moresonsense
Now I want to replace the word morenonsense to D:\tree\path\nonsens\oink.txt. This should be the result
VAR=D:\tree\path\nonsens\oink.txt
For this, I am using the tool sed for windows. But using sed in windows gives me the following:
VAR=D: ree/path/nonsens/oink.txt
The spaces between the colon and ree is a tab. I thought, I could fix it with the following line before calling sed:
SET "somevar=%somevar:\\=\\\\%"
But no, this line is not working. So I have some questions:
Is there a possibility, to prevent sed from changing \t to a tab and prevent changing two backslashed \ to a slash /?
Is there another easy way to replace a string with another string within a file with BATCH?
Does someone has another idea how to resolve this problem?
You should not \-escape the \ instances in the variable expansion; use the following:
SET "somevar=%somevar:\=\\%"
I don't know whether that solves all your problems, but SET "somevar=%somevar:\\=\\\\%" definitely does not work as intended, because it'll only match two consecutive \ chars in the input, resulting in a no-op with your input.

Shortcut in nano editor for adding quotation marks to every word beginning with $ in a bash script?

I am new to writing in bash and I just finished this long script but I made the mistake of not adding quotation marks to all the variables beginning with the unary operator $. Adding all the quotation marks by hand is going to take a while. Is there a short cut I can use so all the words in the text file beginning with $ get quotation marks around them? So if a line in the file looks like:
python myProgram.py $car1 $car2 $speed1 $speed2
Then after the shortcut it will appear as
python myProgram.py "$car1" "$car2" "$speed1" "$speed2"
I am writing the script using nano.
Use global search and replace with the expression (\$\w+).
Switch to search and replace mode with C-\.
Switch to regex mode with Alt-R.
Type the expression (\$\w+). Hit Enter.
Type in the replacement expression "\1" replace the captured expression with quotations. Hit Enter.
On the match, hit A for All.
Given your need, it doesn't seem mandatory to provide a solution based on that editor. If you have access to a shell you might try this simple sed command:
sed -i.bak -r 's/\$\w+/"&"/g' my-script.sh
This is far from being perfect but should do the job in your particular case. If the above command:
-i.bak will perform the replacement "in place" -- that is modifying the original file, making a backup with the .bak extension
s/..../..../g is the usual sed command to search and replace using a pattern. The search pattern is between the first two \. The replacement is between the last two /
\$\w+ this pattern correspond to a $ followed by one or more letters (\w+). The backslash before $ is needed because that character normally has special meaning in a search pattern.
"&" is the replacement string. In there, the & is replaced by the string found in the search pattern. Broadly speaking this put quotes arround any string matching the search pattern.

Absolute path in perl's copy command gone wrong?

This is my very simple code, which isn't working, for some reason I can't figure out.
#!/usr/bin/perl
use File::Copy;
$old = "car_lexusisf_gray_30inclination_000azimuth.png";
$new = "C:\Users\Lenovo\Documents\mycomp\simulation\cars\zzzorganizedbyviews\00inclination_000azimuth\lexuscopy.png";
copy ($old, $new) or die "File cannot be copied.";
I get the error that the file can't be copied.
I know there's nothing wrong with the copy command because if I set the value of $new to something simple without a path, it works. But what is wrong in the representation of the path as I've written it above? If I copy and past it into the address bar of windows explorer, it reaches that folder fine.
Tip: print out the paths before you perform the copy. You'll see this:
C:SERSenovodocumentsmycompsimulationrszzzorganizedbyviewsinclination_000azimuthexuscopy.png
Not what we wanted. The backslash is an escape character in Perl, which needs to be escaped itself. If the backslash sequence does not form a valid escape, then it's silently ignored. With escaped backslashes, your string would look like:
"C:\\Users\\Lenovo\\Documents\\mycomp\\simulation\\cars\\zzzorganizedbyviews\\00inclination_000azimuth\\lexuscopy.png";
or just use forward slashes instead – in most cases, Unix-style paths work fine on Windows too.
Here is a list of escapes you accidentally used:
\U uppercases the rest
\L lowercases the rest
\ca is a control character (ASCII 1, the start of heading)
\00 is an octal character, here the NUL byte
\l lowercases the next character.
If no interpolation is intended, use single quotes instead of double quotes.

Take in escaped input in Ruby command line app

I'm writing a Ruby command line application in which the user has to enter a "format string" (much like Date.strptime/strftime's strings).
I tried taking them in as arguments to the command, eg
> torque "%A\n%d\n%i, %u"
but it seems that bash actually removes all backslashes from input before it can be processed (plus a lot of trouble with spaces). I also tried the highline gem, which has some more advanced input options, but that automatically escapes backslashes "\" -> "\\" and provides no alternate options.
My current solution is to do a find-and-replace: "\\n" -> "\n". This would take care of the problem, but it also seems hacky and awful.
I could have users write the string in a text file (complicated for the user) or treat some other character, like "&&", as a newline (still complicated for the user).
What is the best way for users to input escaped characters on the command line?
(UPDATE: I checked the documentation for strptime/strftime, and the format strings for those functions replace newline characters with "%n", tabs with "%t", etc. So for now I'm doing that, but any other suggestions are welcome)
What you're looking for is using single quotes instead of double quotes.
Thus:
> torque '%A\n%d\n%i, %u'
Any string quoted in single quotes 'eg.' is does not go through any expansions and is used as is.
More details can be found in the Quoting section of man bash.
From man bash:
Enclosing characters in single quotes preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.
p eval("\"#{gets.chomp}\"")
Example use:
\n\b # Input by the user from the keyboard
"\n\b" # Value from the script
%A\n%d\n%i, %u # Input by the user from the keyboard
"%A\n%d\n%i, %u" # Value from the script

Resources