sed linux command unable to insert line [duplicate] - regex

This question already has answers here:
How to escape single quotes within single quoted strings
(25 answers)
Closed 4 years ago.
How can I make this regular expression work correctly. Is adding the line
LOGIN_SERVICE: '"https://dev-login-o365.grey.com/gp_loginservice/"',
in the file env.js I'm using the regular expresion:
sed -i '5i LOGIN_SERVICE: '"https://login.xxxx.com/server/"',' ./env.js
But is adding the value without the quotes and 3 spaces more to the left like this:

Use different quoting:
sed -i "5i LOGIN_SERVICE: '\"https://login.xxxx.com/server/\"'," env.js
Or as the helpful comments below suggest, wrap sed command in single quote:
sed '5i LOGIN_SERVICE: '\''"https://login.xxxx.com/server/"'\'',' env.js

Related

Regex to extract string between two sets of underscores [duplicate]

This question already has answers here:
How to get String between last two underscore
(5 answers)
Closed 2 years ago.
I have been trying to extract a string for a directory path that contains multiple underscores as delimiters.
I'm trying on regex101 to extract foobar but can only get _pdf-documents_
regex
_([^_]+)_
directory path
/data/documents/2020/05/07/2020-05-07-12_pdf-documents_foobar_hour.abc.defg.log
If you work only with this string you can use this _([^_p]+)_
If awk is ok, then :
echo '/data/documents/2020/05/07/2020-05-07-12_pdf-documents_foobar_hour.abc.defg.log' |
awk -F'_' '{print $3}'
Output
foobar
Or like said Wiktor Stribiżew in comments, split like I do in another language, this is the most simple, maintainable, readable and reliable solution
This worked for me.
.*_([^_]+)_.*

Why sed command does not recognize my regex to change date format? [duplicate]

This question already has answers here:
What delimiters can you use in sed? [duplicate]
Using different delimiters in sed commands and range addresses
(3 answers)
How to insert strings containing slashes with sed? [duplicate]
(11 answers)
Closed 2 years ago.
I'm studying Regex and I faced a example that convertes dates formats:
sed -r 's|([0-9]{2})/([0-9]{2})/([0-9]{4})|\3/\2/\1|' <<< 05/04/1947
Output:
1947/04/05
I understand why this works but I'm not sure why the author used | 'pipe' instead of '/' to separate the regexp, replacement and flags sections.
I think changing | to / would make the same output:
sed -r 's/([0-9]{2})/([0-9]{2})/([0-9]{4})/\3/\2/\1/' <<< 05/04/1947
Why the last example does not work like the first one?
Thanks!

Use variable in replace regex [duplicate]

This question already has answers here:
Sed replace variable in double quotes
(3 answers)
Closed 3 years ago.
I've already looked at the following:
Escape a string for a sed replace pattern
Is it possible to escape regex metacharacters reliably with sed
They give absolutely no easy to understand answer.
version=1.2.3
sed -i -z -e 's/"version": "[A-Za-z0-9_.-]*"/"version": "$(version)"/' package.json
I'm trying to use a variable in a replace regular expression in a file. I don't have to use sed per say, as long as it works on macs and linux dists I'm ok with it.
So I just had to use double quote for sed to recognise the 4version variable, then just escape the double quotes which are included otherwise.
sed -i -z -e "s/\"version\": \"[A-Za-z0-9_.-]*\"/\"version\": \"$version\"/" package.json

sed not capturing regex [duplicate]

This question already has answers here:
Why doesn't `\d` work in regular expressions in sed? [duplicate]
(3 answers)
Closed 4 years ago.
I'm attempting to replace the beginning of lines in a simple file.
>1
TGAACCATCGAGTCTTTGAACG
>2
GAGTTCATTTCTCTCTGGAGGCACC
>3
ATTGACAGATTGAGAGCTCTTTC
>4
CGGGAAAAGGATTGGCTC
>5
TCTTGGTGGTAGTAGCAAATATTCAAATG
Above is the input, below is the desired output.
>seq_x1
TGAACCATCGAGTCTTTGAACG
>seq_x2
GAGTTCATTTCTCTCTGGAGGCACC
>seq_x3
ATTGACAGATTGAGAGCTCTTTC
>seq_x4
CGGGAAAAGGATTGGCTC
>seq_x5
TCTTGGTGGTAGTAGCAAATATTCAAATG
Here is the command I've tried using:
sed -n -r 's/^>(\d+)/>seq_x\1/' file
Using text editors on a small subset of the file, I have no problem with find and replace using '^>(\d+)' and '>seq_x\1', but I can't get sed to replace effectively. Is there something I'm missing?
In your simple case there's no need to specify a followed digit. Besides, you should use a more portable [0-9] (range of numbers) instead of \d+.
sed 's/^>/>seq_x/' file

How to use sed and/or regex to trim a line in a file using bash? [duplicate]

This question already has answers here:
Regex to extract first 3 words from a string
(3 answers)
Closed 5 years ago.
This seems like it should be simple, but I've spent far too much time searching. How can I use sed and regex to trim off all words in a line after the fourth word?
For instance from:
19900101, This is a title
19091110, This is a really long title
I would like to have
19900101, This is a
19091110, This is a
I've tried answers like this one Regex to extract first 3 words from a string, but I'm using Mac OSX, so I get context address errors.
This is easily done using cut:
cut -d ' ' -f 1-4 file
19900101, This is a
19091110, This is a
Or using awk:
awk '{NF=4} 1' file
19900101, This is a
19091110, This is a
This might work for you (GNU sed):
sed 's/\s*\S*//5g' file
Remove the fifth or more words from the line.