Escape dollar sign in regexp for sed

I will tell you what my question is about before actually asking - do not hesitate to skip this section!

Some information about my setup

To update files manually in the software system, I create a bash script to remove all files that are not in the new version using diff:

for i in $(diff -r old new 2>/dev/null | grep "Only in old" | cut -d "/" -f 3- | sed "s/: /\//g"); do echo "rm -f $i" >> REMOVEOLDFILES.sh; done

It works great. However, apparently, my files often have a dollar sign ($) in the file name, this is due to some permutations of the GWT structure. Here is one sample line from the bash script created above:

rm -f var/lib/tomcat7/webapps/ROOT/WEB-INF/classes/ExampleFile$3$1$1$1$2$1$1.class

Running this script will not delete the necessary files, because bash reads them as argument variables. Therefore, I need to escape the dollar sign with "\ $".

My actual question

Now I want to add sed-Command to the above pipeline, replacing this dollar sign. In fact, sed also reads the dollar sign as a special character for regular expressions, so obviously I need to avoid it too. But for some reason this does not work, and I could not find an explanation after the multiplayer game.

Here are some options I've tried:

echo "Bla$bla" | sed "s/\$/2/g"        # Output: Bla2
echo "Bla$bla" | sed 's/$$/2/g'        # Output: Bla
echo "Bla$bla" | sed 's/\\$/2/g'       # Output: Bla
echo "Bla$bla" | sed 's/@"\$"/2/g'     # Output: Bla
echo "Bla$bla" | sed 's/\\\$/2/g'      # Output: Bla

The desired result in this example should be "Bla2bla". What am I missing? I am using GNU sed 4.2.2

EDIT

I just realized that the above example is wrong for a start - the echo command already interprets the $ variable as a variable, and the following sed does not get it anyway ... Here is the correct example:

  • Create a text file testwith the contentsbla$bla
  • cat test gives bla$bla
  • cat test | sed "s/$/2/g" gives bla$bla2
  • cat test | sed "s/\$/2/g" gives bla$bla2
  • cat test | sed "s/\\$/2/g" gives bla2bla

, - . : , , .

+4
3

script, , $, , rm script.

echo "rm -f '$i'" >> REMOVEOLDFILES.sh

printf, :

printf "rm -f '%s'" "$i" >> REMOVEOLDFILES.sh

( , , .)

+3

sed - double-backslash. :

cat filenames.txt | sed "s/\\$/\\\\$/g" > escaped-filenames.txt

, . : bla$1$2.class bla\$1\$2.class. :

for i in $(diff -r old new 2>/dev/null | grep "Only in old" | cut -d "/" -f 3- | sed "s/: /\//g" | sed "s/\\$/\\\\$/g"; do echo "rm -f $i" >> REMOVEOLDFILES.sh; done

chepner , . , $-signs bash script, :

for i in $(diff -r old new 2>/dev/null | grep "Only in old" | cut -d "/" -f 3- | sed "s/: /\//g"); do echo "rm -f '$i'" >> REMOVEOLDFILES.sh; done

( echo "rm -f '$i'" )

+1

, - !

, : (, PS1 .bashrc). .

$ DOLLAR_TERMINATED="123456 $"
$ echo "${DOLLAR_TERMINATED}" | sed -e "s/ \\$/END/"
123456END
$ echo "${DOLLAR_TERMINATED}" | sed -e "s/ \\$$/END/"
sed: -e expression #1, char 13: Invalid back reference
$ echo "${DOLLAR_TERMINATED}" | sed -e "s/ \\$\s*$/END/"
123456END

, :

  • DOLLAR_TERMINATED - DOLLAR_TERMINATED "END"
  • ,
  • , ( $ )
  • , ()

( sed 4.2.2 2016 , bash - 4.3.48(1)-release (x86_64-pc-linux-gnu), )

0

Source: https://habr.com/ru/post/1629564/


All Articles