Search and Replace with Sed When Dots and Underscores Are Present

Use sed with variable that contains dot

Using an inner sed:

sed -i "s@$(echo $previousName | sed 's/\./\\./g')@$newName@g" myFile

replace string with underscore and dots using sed or awk

Your regex doesn't really feel too much more general than the fixed pattern would be, but if you want to make it work, you need to allow for more than one lower case character between each dot. Right now you're looking for exactly one, but you can fix it with \+ after each [[:lower:]] like

printf '%s' "$stringZ" | sed -e 's/\([[:lower:]]\+\.[[:lower:]]\+\.[[:lower:]]\+\.[[:lower:]]\+\.[[:lower:]]\+\.[[:lower:]]\+\.[[:lower:]]\+\.\)//g'

which with

stringZ="META_ALL_whrAdjBMI_GLOBAL_August2016.bed.nodup.sortedbed.roadmap.sort.fgwas.gz.r0-ADRL.GLND.FET-EnhA.out.params"

give me the output

META_ALL_whrAdjBMI_GLOBAL_August2016.r0-ADRL.GLND.FET-EnhA.out.params

Replace dots with underscores in right part of the line

You can use a loop:

sed ':a;s/\({{[^}]*\)\./\1_/;ta' file

:a defines a label "a"

ta jumps to "a" when something is replaced.

Sed:Replace a series of dots with one underscore

sed speaks POSIX basic regular expressions, which don't include + as a metacharacter. Portably, rewrite to use *:

sed 's/\.\.*/_/'

or if all you will ever care about is Linux, you can use various GNU-isms:

sed -r 's/\.\.*/_/'    # turn on POSIX EREs (use -E instead of -r on OS X)
sed 's/\.\+/_/' # GNU regexes invert behavior when backslash added/removed

That last example answers your other question: a character which is literal when used as is may take on a special meaning when backslashed, and even though at the moment % doesn't have a special meaning when backslashed, future-proofing means not assuming that \% is safe.

Additional note: you don't need two separate sed commands in the pipeline there.

echo $name | sed -e 's/\%20/_/' -e 's/\.+/_/'

(Also, do you only need to do that once per line, or for all occurrences? You may want the /g modifier.)

bash shell reworking variable replace dots by underscore

You can combine pattern substitution with tr:

VERSION=$( echo ${VERSIONNUMBER:1} | tr '.' '_' ) 

Sed replace hyphen with underscore

You can simplify things by using two distinct regex's ; one for matching the lines that need processing, and one for matching what must be modified.

You can try something like this:

$ sed '/^leaf/ s/-/_/' file
dont-touch-these-hyphens
leaf replace_these-hyphens

Sed replace string with another string wrong result

This should do:

echo "<html xmlns:og="http://test.org/schema/"  xmlns:website="../test/ns/website" >" | sed 's|\.\./|./|g'
<html xmlns:og=http://test.org/schema/ xmlns:website=./test/ns/website >

You need to escape the . or else it mean any character.

This should work as well:
sed 's+\.\./+./+g'



Related Topics



Leave a reply



Submit