sed to remove a user in svn access control list - linux

Filename: stackgroup.acl
[groups]
stackoverflow=linus,steve,bill,adrian
stackexchange=charlie,darwin,carol,kelly
I need an sed code that could remove a user whether it's in the start of the line, or the end of it.
Here's what I got so far:
sed 's/\(.*=*\)linus,\(.*\)/\1\2/g'
sed 's/\(.*=*\)steve,\(.*\)/\1\2/g'
sed 's/\(.*=*\),adrian\(.*\)/\1\2/g'
as you can see, the middle one is fine, but the first and last user will leave an additional comma.
I even tried using regex:
sed 's/\(.*=*\),\?linus,\?\(.*\)/\1\2/g'
or
sed 's/\(.*=*\),*linus,*\(.*\)/\1\2/g'
but it's not working.
Can anyone help?

Use two expressions, the 2nd one takes care of the edge case where the name is directly after the =
#!/bin/bash
user="linus"
sed "s/,\?$user//;s/=,/=/" stackgroup.acl

This might work for you:
v1=charlie;v2=darwin;v3=kelly
sed s'/'"$v1"',\?\|,'"$v1"'$//' <<<"stackexchange=charlie,darwin,carol,kelly"
stackexchange=darwin,carol,kelly
sed s'/'"$v2"',\?\|,'"$v2"'$//' <<<"stackexchange=charlie,darwin,carol,kelly"
stackexchange=charlie,carol,kelly
sed s'/'"$v3"',\?\|,'"$v3"'$//' <<<"stackexchange=charlie,darwin,carol,kelly"
stackexchange=charlie,darwin,carol
sed s'/'"$v3"',\?\|,'"$v3"'$//' <<<"stackexchange=kelly"
stackexchange=

Related

How to use sed to replace a filename in text file

I have a file:
dynamicclaspath.cfg
VENDOR_JAR=/clear-as-1-d/apps/sterling/jar/struts/2_5_18/1_0_0/log4j-core-2.10.0.jar
VENDOR_JAR=/clear-as-1-d/apps/sterling/jar/log4j/2_17_1/log4j-core-2.10.0.jar
I want to replace any occurrence of log4j-core* with log4j-core-2.17.1.jar
I tried this but I know I'm missing a regex:
sed -i '/^log4j-core/ s/[-]* /log4j-core-2.17.1.jar/'
With your shown samples please try following sed program. Using -E option with sed to enable ERE(extended regular expressions) with it. In main program using substitute option to perform substitution. Using sed's capability to use regex and store matched values into temp buffer(capturing groups). Matching till last occurrence of / and then matching log4j-core till jar at last of value. While substituting it with 1st capturing group value(till last occurrence of /) followed by new value of log4j as per OP's requirement.
sed -E 's/(^.*\/)log4j-core-.*\.jar$/\1log4j-core-2.17.1.jar/' Input_file
Using sed
$ sed -E 's/(log4j-core-)[0-9.]+/\12.17.1./' input_file
VENDOR_JAR=/clear-as-1-d/apps/sterling/jar/struts/2_5_18/1_0_0/log4j-core-2.17.1.jar
VENDOR_JAR=/clear-as-1-d/apps/sterling/jar/log4j/2_17_1/log4j-core-2.17.1.jar
It depends on possible other contents in your input file how specific the search pattern must be.
sed 's/log4j-core-.*\.jar/log4j-core-2.17.1.jar/' inputfile
or
sed 's/log4j-core-[0-9.]*\.jar/log4j-core-2.17.1.jar/' inputfile
or (if log4j-core*.jar is always the last part of the line)
sed 's/log4j-core.*/log4j-core-2.17.1.jar/' inputfile
sed -i s'#2.10.0.jar$#2.17.1.jar#'g file
That seems to work.

How to make GNU sed remove certain characters from a line

I have a following line;
�5=?�#A00165:69:HKJ3YDMXX:1:1101:16812:7341 1:N:0:TCTTAAAG
and would like to remove characters, �5=?� in front of #. So the desired output looks as follows;
#A00165:69:HKJ3YDMXX:1:1101:16812:7341 1:N:0:TCTTAAAG
I used gnu sed (v4.8)with a following argument;
sed "s/.*#/#/"'
but this did not remove �5=?� thought it worked in the GNU sed live editor.
At this point, I really appreciate any help on this.
My system is 3.10.0-1160.71.1.el7.x86_64
Using sed, remove everything up to the first occurance of #
$ sed 's/^[^#]*//' input_file
#A00165:69:HKJ3YDMXX:1:1101:16812:7341 1:N:0:TCTTAAAG
This might work for you (GNU sed):
sed -E 's/(\o357\o277\o275)5=\?\1//g' file
This removes all occurrences of �5=?�.
N.B. To translate the octal strings use sed -n l file to display the file as is. The triplets \357\277\275 can be matched in the LHS of the substitute command by using \o357\o277\o275.

Delete non-numerical values - Unix

I have a file where I only need numbers [0-9]. I have this command sed 's/[^0-9]*//g' which deletes anything that's not [0-9] but I need to only delete things left of a ","
I have this now, but it isn't working how I'd think it should...
sed -ri "s/[^0-9]+\,/,/g"
As I said in the comment above, I don't understand why 's/[^0-9].\(.*,\)/\1/g' doesn't work, but there are alternatives.
We can use t (test) to do what I expected g to do:
sed -e:a -e 's/[^0-9].\(.*,\)/\1/;ta'
Or use the hold space (overkill, but it works):
sed 'h;s/.*,/,/;x;s/,.*//;s/[^0-9].//g;G;s/\n//'

Conditional replace using sed

My question is probably rather simple. I'm trying to replace sequences of strings that are at the beginning of lines in a file. For example, I would like to replace any instance of the pattern "GN" with "N" or "WR" with "R", but only if they are the first 2 characters of that line. For example, if I had a file with the following content:
WRONG
RIGHT
GNOME
I would like to transform this file to give
RONG
RIGHT
NOME
I know i can use the following to replace any instance of the above example;
sed -i 's/GN/N/g' file.txt
sed -i 's/WR/R/g' file.txt
The issue is that I want this to happen only if the above patterns are the first 2 characters in any given line. Possibly an IF statement, although i'm not sure what the condition would look like. Any pointers in the right direction would be much appreciated, thanks.
just add the circumflex, remove g suffix (unnecessary, since you want at most one replacement), you can also combine them in one script.
sed -i 's/^GN/N/;s/^WR/R/' file.txt
Use the start-of-string regexp anchor ^:
sed -i 's/^GN/N/' file.txt
sed -i 's/^WR/R/' file.txt
Since sed is line-oriented, start-of-string == start-of-line.

I am having trouble with Sed

I am trying to use the sed command to replace this line:
charmm.c36a4.20140107.newcali4.fixhcali.grange.b
with:
charmm.20140911.c36a4.3rd.ghost2.model3rd
When I use:
sed -i '/s/firstline/secondline/g'
It doesn't work. I think the periods are messing it up. How do I get around this?
sed uses regular expressions, so . matches any character. If you want to only match the . character itself, tell sed to look for \.
so to change the first line into the second line:
sed -e 's/charmm\.c36a4.20140107\.newcali4\.fixhcali\.grange\.b/charmm.20140911.c36a4.3rd.ghost2.model3rd/g' < filetochange >newfile
Here, I added "g" so it does it globally, ie, if there are several instances on the same line, all will be changed. If you remove the "g", it will only change the first occurence on each line.
It reads from filetochange and writes to newfile
If you do :
sed -i -e 's/charmm\.c36a4.20140107\.newcali4\.fixhcali\.grange\.b/charmm.20140911.c36a4.3rd.ghost2.model3rd/g' filetochange
it will directly do the change in "filetochange" ... but please be careful, a badly written sed -i could mess up the file and make it unusable
The s command follows this syntax:
s/pattern/replacement/
You need to drop the / in front of the sed command.

Resources