Shell scripting - run a regular expression to modify a file in place, including the file name

I have many html files named 12345.html, 12346.html, etc. I need to change "style.css" to "style-12345.css" or the corresponding file name. I'm not sure which tool to use, recommendations?

+3
source share
3 answers

This is pretty easy to do with a for and sed loop:

for i in *.html; do
    sed -i "s/style\\.css/style-`basename $i .html`.css/g" $i
done

The loop executes an internal c command $i, set for each .html file name. sed -imodifies the file in place. basename $i .htmlgets $iwithout a suffix .html(i.e. just a number)

+3
source

rename. , .

perl /usr/bin/prename, perl . ,

$ prename 's/foo/bar/ *foo* 

"foo" "bar" , "foo".

util-linux /usr/bin/rename, . ,

$ rename foo bar *foo*

, .

prename rename, , .

+1
perl -pi -e 's/style.css/style-12345.css/g' *.html
0
source

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


All Articles