BASH: sed to remove "/" from a $STRING [duplicate] - string

This question already has answers here:
How to insert strings containing slashes with sed? [duplicate]
(11 answers)
Closed 7 years ago.
First let me apologize for asking a question that:
Has some examples, though I find them confusing
Has a man page, also find confusing
Problem:
I would like to replace text in a $STRING within bash for a script I am writing. I chose to combine date/time to allow for easier end user integration.
STARTTIME="2015-03-17/11:30:00"
sed "Unknown"
Attempted Solution:
sed '/s// /' "$STARTTIME"
Desired result is to remove the "/" and end up with 2015-03-17 11:30:00 to then be passed to a command.
Thank you for any assistance.

If you're using bash, I would suggest that you used built-in string manipulation:
$ s='2015-03-17/11:30:00'
$ echo "${s/\// }"
2015-03-17 11:30:00
The syntax inside the braces means "replace the first occurrence of a forward slash (which needs escaping) with a space".

Related

Add markdown code tick to beginning and ending of a variable [duplicate]

This question already has an answer here:
How to escape backticks in bash
(1 answer)
Closed 3 months ago.
I searched all forums but didn't find a suitable way to achieve this.
variable
result=multiline
string and
other string
Want to convert it to
```result=multiline
string and
other string```
means add 3 ticks at the beginning and 3 ticks at the end of the file.
I know it's possible through sed, but I think it's not working because of special characters for example tick.
Any advice is appreciated.
Escaping backticks with \ helped.
echo $result > result.txt
echo "\`\`\`" | cat - result.txt > some-result.txt
echo "\`\`\`" >> markdown-result.txt

Replace hyphens with underscores in bash script [duplicate]

This question already has answers here:
Replacing some characters in a string with another character
(6 answers)
Difference between sh and Bash
(11 answers)
Closed 4 years ago.
Trying to write a bash script and in one part of it I need to take whatever parameter was passed to it and replace the hyphens with underscores if they exist.
Tried to do the following
#!/usr/bin/env bash
string=$1
string=${string//-/_}
echo $string;
It's telling me that this line string=${string//-/_} fails due to "Bad substitution" but it looks like it should do it? Am I missing something?
There is nothing wrong with your script, and it should work in modern versions of Bash.
But just in case you can simplify that to :
#!/bin/bash
echo "$1" | tr '-' '_'
This is in case that parameter substitution does not work ( which seems to be your case ).
Regards!

How to get a substring using sed being the pattern "/" [duplicate]

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 4 years ago.
I have the following string:
/book/A00001/2018/01/15/Chamber_Music
And I want to get using the sed command:
/book/A00001/2018/01/15/
Thanks
Regards
Maybe are you looking for: sed "s/\(.*\)Chamber_Music/\1/g"
No need to use sed, you can use normal shell string handling:
filename='gash.txt'
new_filename="$filename.new"
while read line
do
line=${line%/*}
echo $line
done <"$filename" >"$new_filename"
#mv "$new_filename" "$filename" # Commented out to be optional
Given your input in your second question:
/book/A00001/2018/01/15
/book/A00001/2018/01/15
/book/A00001/2018/01/15
/book/A00001/2018/01/15
/book/A00001/2018/01/15
/book/A00001/2018/01/15
You can change the regexp sed delimiter.... see the s command documentation. If you use, e.g.
sed 's:([a-zA-Z0-9/]*)[a-zA-Z]*$:\1:'
then, the / loses its special treatment and the : character assumes it. Of course, you can store the matching pattern in an environment variable, before to feed it to sed(1), and substitute all / into \/ to escape every /.
pattern=`echo "([a-zA-Z0-9/])[a-zA-Z]*\$" | sed 's/\//\\//'`
sed "s/${pattern}/\\1/"
but this is more complex.

How to delete last found value in Bash [duplicate]

This question already has answers here:
Bash : extracting part of a string
(4 answers)
Closed 6 years ago.
Say I have a string 0.0.25, how do I delete the last part after dot (including it) to make it like 0.0? Note that last part can have variable number of digits.
In bash you can do the following:
echo "${var%.*}"
See the Shell Parameter Expansion section of the manual.
Using awk you could:
echo "0.0.25" | awk -F. '{print $1"."$2}'

Why is SED echoing rather than editing: sed '/^;text1/!b;:a;n;//ba;i\text2' [duplicate]

This question already has answers here:
How do I use a new-line replacement in a BSD sed?
(4 answers)
Closed 7 years ago.
sed '/^;date.timezone =/!b;:a;n;//ba;i\date.timezone = Europe/London' /etc/php.ini
You can probably guess Im creating a script for setting up LAMP servers.
In the above example the text is not replaced but instead the changes are displayed on the console.
I my goal was to insert date.timezone = Europe/London the last occurence of ;date.timezone =
Etan Reisner:
You aren't using the -i flag to tell sed to modify in place.

Resources