sed command to delete starting pattern match with special character in it - linux

I have sed command that delete line when string match with starting pattern
sed -i "/^Data\=C\b/Id" tally.ini
This work correctly for patch Data=C
But I want to match path Data=C:\ and that I want to delete this line.
How can this possible?

sed -i '/^Data\=C:\\/Id' tally.ini
Put the command in a single quoted string instead of double.
Escape the backslash.

Related

How to add text with sed at the end-of-line minus 1 position

I want to add the text [*,test,*] just before the last character of a line starting with resourceFilters: in the input file (that string can be preceded by any number of spaces).
An example of this line is the following
resourceFilters: '[Event,*,*][*,kube-system,*][*,kube-public,*][*,kube-node-lease,*][Node,*,*][APIService,*,*][TokenReview,*,*][SubjectAccessReview,*,*][Binding,*,*][ReplicaSet,*,*]'
which would be changed into this:
resourceFilters: '[Event,*,*][*,kube-system,*][*,kube-public,*][*,kube-node-lease,*][Node,*,*][APIService,*,*][TokenReview,*,*][SubjectAccessReview,*,*][Binding,*,*][ReplicaSet,*,*][*,test,*]'
sed "/^ *resourceFilters: '/s/'$/[*,test,*]'/" your_file
I've double quoted the Sed command so that I can use single quotes in it;
/^resourceFilters: '/ makes the following substitution command only act on lines starting with resourceFilters: ';
s/'$/[*,test,*]'/ is the substitution command which matches ' at end of line ($), and substitutes [*, test,*]' to it, effectively inserting [*,test,*] before the '.
This might work for you (GNU sed):
sed -E 's/^(\s*resourceFilters:.*)(.)$/\1[*,test,*]\2/' file
Match on a line containing resourceFilters: and insert [*,test,*] before the last character of the line.

Why sed script is not deleting lines?

The following is my sed script:
#!/bin/bash
sed -r 's/^\s+?\/\/.*$/d/;
s/LOG.debug/System.out.println/g' "$1"
What the first command should do is delete all lines beginning with // preceded by any number of spaces or tabs. The only problem is instead of deleting those lines, it replaces them with a literal 'd'.
If you need to remove the matching lines, you should not use the s command.
Use
sed '/^[[:blank:]]*\/\//d; s/LOG\.debug/System.out.println/g' "$1"
# ^^^^^^^^^^^^^^^^^^^^
Also, note that to match a literal dot, you need to escape it, hence, LOG\.debug.
See an online sed demo.

Add many quotation in sed instruction

I have to update line with value containg many quotation characters
sed -i 's/.*sonar.links.issue.*/property "sonar.links.issue", "http://jra.url"'
I get error there. What should i do to do it correctly?
Your replacement string contains slashes "http://", so you shouldn't use / as the delimiter for this sed command, use # as shellter proposes.
To complete its answer, I would add the "g" flag, at the end of the sed expression, to ensure the subsitution is made on the whole line, and not only on the first match with ".sonar.links.issue.".
sed -i 's#.*sonar.links.issue.*#property "sonar.links.issue", "http://jra.url"#g' file
Try
sed -i 's#.*sonar.links.issue.*#property "sonar.links.issue", "http://jra.url"#' file
IHTH

delete and replace a line using linux command

I am trying to delete a line with the pattern matches and replacing the entire line with the another line using sed command.
File contents:Sample.txt
Testfile=xxxx
Testfile3=uuuu
Testfile4=oooo
Testfile5=iiii
Testfile2=ikeii
I am using sed command to delete a line contains Testfile3=* and replace by Testfile3=linechanged
sed -i 's/Testfile3=\*/Testfile3=linechanged/' Sample.txt.
But it just appends the replaceable string in the line as shown below
Testfile3=linechanged=uuuu.
I am expecting the output to be
Testfile3=linechanged.
What i am doing wrong?
The star is not matched right:
sed -i 's/Testfile3=.*/Testfile3=linechanged/' Sample.txt
# ^^
.* matches any character (.) for any length (*), so it will match everything till the end of the line.
You can use captured group to keep what will be preserved and use the desired replacement for the rest:
sed -i 's/^\(Testfile3=\).*/\1linechanged/' file.txt
In your case, escaping the Regex token * like \* will match * literally e.g. Testfile3=* would be matched then.
This might work for you (GNU sed):
sed '/Testfile3/cTestfile3=linechanged' file
This matches the line containing Testfile3 and changes it to the required string.

Replace first sign in line (linux bash)

I want to delete first sign in a file (without creating new file). That is the line (and this line isn't the first one or last one):
#$config['rrdcached'] = "unix:/var/run/rrdcached.sock";
I'm trying to do thuis with sed command but it doesn't work. That is my command:
sed -i "s/#$config\['rrdcached'\].*$/$config\['config'\]/g" text.txt
Any suggestions?
Just replace the first match of # with following command:
sed -i '1 s/#//' test.txt
The $ characters are causing two problems.
First, the shell is treating $config as a variable reference, and replacing it with the value. You need to escape the $ to prevent that.
Second, $ has special meaning in regular expressions, so you need to escape it at that level as well. So you need to escape the backslash and the $.
sed -i "s/^#\\\$config\['rrdcached'\].*\$/\$config['config']/" text.txt
There's no need for the g modifier since you only want to replace the first match on the line. And you should use the ^ anchor so it only matches this at the beginning of the line.
It's also not necessary to escape special regexp characters in the replacement string.
This command works, but i forgot that there is something after '='. At now, everything after that is deleted.
I wrote this:
sed -i "s/^#\\\$config\['rrdcached'\] = "unix:/var/run/rrdcached.sock";.*\$/\\\$config\['config'\]/ = "unix:/var/run/rrdcached.sock";" text.txt

Resources