简体   繁体   English

如何从Unix shell脚本中的字符串中删除字符?

[英]How do I remove a character from a string in a Unix shell script?

I have several files that are named like this: file - name.txt 我有几个文件名如下:file - name.txt

How do I remove the " - " using a bash script in UNIX from all the files? 如何在UNIX中使用bash脚本从所有文件中删除“ - ”?

如果我理解正确,请尝试rename ' - ' '' *

Sed it up! 坚持下去!

# Iterate each file in the current directory.
for i in *; do
  # Move the file to the new filename, replacing ' - ' with '_'
  mv "$i" `echo $i | sed 's/ - /_/g'`
done

Use parameter expansion to remove the part of the string you want to be rid of. 使用参数扩展删除要删除的字符串部分。 Make sure to use double-quotes to prevent mv from misinterpreting input. 确保使用双引号来防止mv误解输入。

for i in ./*' - '*; do
    mv "$i" "${i// - }"
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM