How do I escape "#g" in sed (with the -global setting)? [duplicate] - shell

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 12 months ago.
I have many repositories with the remote origin set to HTTPS. Now I want to change all origin remotes to SSH.
I am using a command for this in which I want to replace all preceeding url = https://gitlab.mypath/ with git#gitlab.maypath:.
Is there a way to express this with one sed call. Something like:
's#https://gitlab.mypath/#git#gitlab.mypath:#g'
I have to be able to escape the first "#g"

The correct anwers
Switch from s### to s|||
Apart from choosing any character for the delimiter, you can also escape that character: #
The explanation
The character after the s specifies the separator, which must occur three times in your s command.
Thanks
#Cyrus #dan

Related

Git bash on Windows different result than terminal on CentOS for regex [duplicate]

This question already has answers here:
Removal of special characters from string using perl script
(2 answers)
Closed 9 months ago.
See the following cleanCustomer.sh file
#!/bin/bash
customer=Reportçós
cleanedCustomer=${customer//[^a-zA-Z0-9 \-_.]/}
echo $cleanedCustomer
When I run it on Windows 11 in Git Bash it prints Reports.
When I run it on CentOS in terminal it prints Reportçós.
Anybody knows why is a-z interpreted as alpha characters in CentOS and not in Windows?
How do I ensure only english characters are considered in the CentOS?
From the bash manual:
A pair of characters separated by a hyphen denotes a range expression; any character that falls between those two characters, inclusive, using the current locale’s collating sequence and character set, is matched. If the first character following the ‘[’ is a ‘!’ or a ‘^’ then any character not enclosed is matched. A ‘-’ may be matched by including it as the first or last character in the set.
Your Git Bash locale uses rules that don't match accented characters in ranges like a-z, your CentOS locale does. This can be addressed by using a consistent locale like C for collation. Plus your - is in the wrong spot; it needs to be first or last, and the backslash needs to be escaped with another backslash to match a literal one.
#!/bin/bash
LC_COLLATE=C
customer=Reportçós
cleanedCustomer=${customer//[^a-zA-Z0-9 \\_.-]/}
printf "%s\n" "$cleanedCustomer"

bash variable eats multiple spaces, turning them to one [duplicate]

This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 3 years ago.
# export var="many spaces"; echo =${var}=
=many spaces=
What is going on here?
Why multiply spaces are turned to one? How to keep all?
You’re simply missing quotations around your variable. Changing your code to this:
$ export var="many spaces"; echo ="${var}"=
=many spaces=
should give the result you’re looking for. One “feature” of bash that you need to watch out for is word splitting, which is based on the value of your IFS (internal field separator) variable. Typically IFS defaults to
IFS=$' \t\n'
so you need to take care in quoting variables that contain spaces, tabs, and newlines.

How to replace quotes inside a quoted field of a non-standard CSV file using a one-liner bash command? [duplicate]

This question already has answers here:
What's the most robust way to efficiently parse CSV using awk?
(6 answers)
Closed 4 years ago.
This post was edited and submitted for review 11 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I have a file like this:
col1×col2×col3
12×"Some field with "quotes" inside it"×"Some field without quotes inside but with new lines \n"
And I would like to replace the interior double quotes with single quotes so the result will look like this:
col1×col2×col3
12×"Some field with 'quotes' inside it"×"Some field without quotes inside but with new lines \n"
I guess this can be done with sed, awk or ex but I haven't been able to figure out a clean and quick way of doing it. Real CSV files are of the order of millions of lines.
The preferred solution would be a one-liner using the aforementioned programs.
A simple workaround using sed, based on your fields separator ×, could be:
sed -E "s/([^×])\"([^×])/\1'\2/g" file
This replace each " which is preceded and followed by any characters other that ×, with '.
Note that sed not support positive lookahead, so we have to group and reinsert the patterns.

UNIX Replace string without replacing space [duplicate]

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
Closed 5 years ago.
For string matching purposes I need to define a bash variable with leading spaces.
I need to define this starting from an integer, like:
jj=5
printf seems to me a good idea, so if I want to fill spaces up to 6 character:
jpat=`printf " %6i" $jj`
but unluckly when I am trying to recall the variable:
echo $jpat
the leading whitespaces are removed and I only get the $jj integer as it was.
Any solution to keep such spaces?
(This is equivalent to this: v=' val'; echo $v$v. Why aren't there leading and multiple spaces in output?)
Use More Quotes! echo "$jpat" will do what you want.
There is another issue with what you're doing: Command substitutions will remove trailing newlines. It's not an issue in the printf command you're using, but for example assigning jpat=$(printf " %6i\n" "$jj") would give you exactly the same result as your command.

How do I truncate the last two characters of all files in a directory? [duplicate]

This question already has answers here:
Bash script to remove 'x' amount of characters the end of multiple filenames in a directory?
(3 answers)
Closed 5 years ago.
So pretty simple question. All of the files in my directory are of the form 6bfefb348d746eca288c6d62f6ebec04_0.jpg. I want them to look like 6bfefb348d746eca288c6d62f6ebec04.jpg. Essentially, I want to take off the _0 at the end of every file name. How would I go about doing this with bash?
With Perl's standalone rename command:
rename -n 's/..(\....)$/$1/' *
If everything looks fine, remove -n.
It is possible to use this standalone rename command with a syntax similar to sed's s/regexp/replacement/ command. In regex a . matches one character. \. matches a . and $ matches end of line (here end of filename). ( and ) are special characters in regex to mark a subexpression (here one . and three characters at the end of your filename) which then can be reused with $1. sed uses \1 for first back-reference, rename uses $1.
See: Back-references and Subexpressions with sed

Resources