如何创建一个bash脚本来降低当前文件夹中的所有文件,然后搜索/替换所有文件?(How to create a bash script that will lower case all files i

编程入门 行业动态 更新时间:2024-10-23 22:31:03
如何创建一个bash脚本来降低当前文件夹中的所有文件,然后搜索/替换所有文件?(How to create a bash script that will lower case all files in the current folder, then search/replace in all files?)

答案#1:我最终从零开始,我能够拼凑出一些有效的东西! 可能不是最有效率的,但它可以完成这项工作。

#!/bin/bash for file in *.php do #Check to see if the filename contains any uppercase characters iscap=`echo $file | awk '{if ($0 ~ /[[:upper:]]/) print }'` if [[ -n $iscap ]] then #If the filename contains upper case characters convert them to lower case newname=`echo $file | tr '[A-Z]' '[a-z]'` #make lower case #Perform various search/replaces on the file name to clean things up newname=$(echo "$newname" | sed 's/---/-/') newname=$(echo "$newname" | sed 's/--/-/') newname=$(echo "$newname" | sed 's/-\./\./') newname=$(echo "$newname" | sed 's/there-s-/theres-/') #Rename file echo "Moving $file\n To $newname\n\n" mv $file $newname #Update all references to the new filename in all php files `sed -i "s/$file/$newname/g" *.php` fi done

答案#2:虽然它没有检查大写字符而只是尝试转换所有内容(对已经小写的文件给出错误),如果你知道你只是要处理带有大写字符的文件你可以使用aqua提交的版本。

#!/bin/bash for file in *.php do newname=`echo $file | tr '[A-Z]' '[a-z]'` newname=$(echo "$newname" | sed 's/---/-/') newname=$(echo "$newname" | sed 's/--/-/') newname=$(echo "$newname" | sed 's/-\./\./') newname=$(echo "$newname" | sed 's/there-s-/theres-/') mv "$file" "$newname" for f in *.php do sed -i "s/$file/$newname/g" *.php done done

感谢大家对此的帮助!


原始问题和附加信息

我正在使用一些软件为开源软件项目创建文档,除了它输出带有大写字符的HTML文件,不推荐用于SEO。 所以,我正在尝试编写一个bash脚本,将所有.html文件更改为小写,然后搜索/替换

所以,我想转此:

这-是最Output.html

进入这个:

这-是最output.html

然后搜索当前目录中的所有文件“This-Is-The-Output.html”并将其替换为“this-is-the-output.html”。

我一直在尝试根据我在网上发现的内容拼凑一些东西,但我无法让它发挥作用。 我对于编写bash脚本缺乏经验,因为有人可以,所以我想我会转向SO看看是否有人可以提供帮助。

这就是我目前正在使用的...

#!/bin/sh for i in *.php; icleaned=`echo $i | sed s/./\\./g`; inew="$(tr [A-Z] [a-z] <<< "$icleaned")"; sed -i 's/$i/$inew/g' *.php; do mv $i $inew; done

第3行:搜索并替换特殊字符,例如“。”

第4行:将干净的小写字符串分配给$ inew

第5行:搜索/替换旧文件名的所有引用到新文件名

第6行:将文件从旧文件名移动到新文件名

我得到的错误是:

line 3: syntax error near unexpected token `icleaned=`echo $i | sed s/./\\./g`' line 3: ` icleaned=`echo $i | sed s/./\\./g`;'

我试着回复icleaned,看看第一步是否正常工作,但我得到完全相同的错误。

我甚至尝试评论除了inew =行之外的所有内容:

#!/bin/sh for i in *.php; # icleaned=`echo $i | sed s/./\\./g`; inew="$(tr [A-Z] [a-z] <<< "$i")"; echo $inew; # sed -i 's/$i/$inew/g' *.php; # do mv $i $inew; done

我仍然得到“意外令牌附近的语法错误”错误。

line 4: syntax error near unexpected token `inew="$(tr [A-Z] [a-z] <<< "$i")"' line 4: ` inew="$(tr [A-Z] [a-z] <<< "$i")";'

很明显,脚本在许多方面都搞砸了。

任何人可以提供的帮助将不胜感激。

Answer #1: I ended up starting from scratch, and I was able to piece something together that works! Might not be the most efficient, but it does the job.

#!/bin/bash for file in *.php do #Check to see if the filename contains any uppercase characters iscap=`echo $file | awk '{if ($0 ~ /[[:upper:]]/) print }'` if [[ -n $iscap ]] then #If the filename contains upper case characters convert them to lower case newname=`echo $file | tr '[A-Z]' '[a-z]'` #make lower case #Perform various search/replaces on the file name to clean things up newname=$(echo "$newname" | sed 's/---/-/') newname=$(echo "$newname" | sed 's/--/-/') newname=$(echo "$newname" | sed 's/-\./\./') newname=$(echo "$newname" | sed 's/there-s-/theres-/') #Rename file echo "Moving $file\n To $newname\n\n" mv $file $newname #Update all references to the new filename in all php files `sed -i "s/$file/$newname/g" *.php` fi done

Answer #2: Although it doesn't check for upper case characters and just tries to convert everything (giving an error on files that are already lower case), if you know you're only going to be processing files with upper case characters you can use the version that aqua submitted.

#!/bin/bash for file in *.php do newname=`echo $file | tr '[A-Z]' '[a-z]'` newname=$(echo "$newname" | sed 's/---/-/') newname=$(echo "$newname" | sed 's/--/-/') newname=$(echo "$newname" | sed 's/-\./\./') newname=$(echo "$newname" | sed 's/there-s-/theres-/') mv "$file" "$newname" for f in *.php do sed -i "s/$file/$newname/g" *.php done done

Thanks for everyone's help on this!


The Original Question & Additional Information

I'm using some software to create documentation for an open source software project, except it outputs the HTML files with upper case characters, which isn't recommended for SEO. So, I'm trying to write a bash script that will change all .html files to lower case, and then search/replace

So, I want to turn this:

This-Is-The-Output.html

Into this:

this-is-the-output.html

And then search all the files in the current directory for "This-Is-The-Output.html" and replace it with "this-is-the-output.html".

I've been trying to piece something together based on what I've found online, but I just can't get it to work. I'm about as inexperienced with writing bash scripts as someone can be, so I figured I would turn to SO to see if someone could help out.

This is what I'm currently working with...

#!/bin/sh for i in *.php; icleaned=`echo $i | sed s/./\\./g`; inew="$(tr [A-Z] [a-z] <<< "$icleaned")"; sed -i 's/$i/$inew/g' *.php; do mv $i $inew; done

Line 3: Do a search and replace for special characters, like "."

Line 4: Assign the clean, lower case string to $inew

Line 5: Search/replace all references of the old filenames to the new filenames

Line 6: Move the files from their old filename to their new filename

The errors I'm getting are:

line 3: syntax error near unexpected token `icleaned=`echo $i | sed s/./\\./g`' line 3: ` icleaned=`echo $i | sed s/./\\./g`;'

I tried just echoing icleaned to see if the first step was working properly, but I get the exact same error.

I even tried commenting out everything except for the inew= line:

#!/bin/sh for i in *.php; # icleaned=`echo $i | sed s/./\\./g`; inew="$(tr [A-Z] [a-z] <<< "$i")"; echo $inew; # sed -i 's/$i/$inew/g' *.php; # do mv $i $inew; done

And I'm still getting the "syntax error near unexpected token" error.

line 4: syntax error near unexpected token `inew="$(tr [A-Z] [a-z] <<< "$i")"' line 4: ` inew="$(tr [A-Z] [a-z] <<< "$i")";'

So obvious the script is messed up in many, many ways.

Any help someone could offer up would be greatly appreciated.

最满意答案

像这样的东西应该工作:

#!/bin/bash for file in *.html do lowercase=`echo $file | tr '[A-Z]' '[a-z]'` mv "$file" "$lowercase" for f in *.html do sed -i "s/$file/$lowercase/g" "$f" done done

如果您正在使用除html文件之外的其他内容,请将*.html更改为*.<extension> 。

Something like this should work:

#!/bin/bash for file in *.html do lowercase=`echo $file | tr '[A-Z]' '[a-z]'` mv "$file" "$lowercase" for f in *.html do sed -i "s/$file/$lowercase/g" "$f" done done

Change *.html to *.<extension> if you're working with something other than html files.

更多推荐

本文发布于:2023-07-22 18:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222328.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   创建一个   脚本   夹中   create

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!