更改文件名后缀(使用sed吗?) [英] Change file name suffix(es) (using sed ?)
问题描述
我想从文件(使用bash脚本)更改文件名后缀,但是有时有些文件带有一个句点,有些带有两个句点.
I'd like to change the file name suffix from files (using a bash script), but sometimes there are files with one period and some with two.
现在我用这个:
new_file=`echo ${file} | sed 's/\(.*\.log.*\)'${suf}'/\1.'${num}'/'`
"new_file"是新文件名,"file"是原始文件名,"$ {suf}"是文件后缀,而$ {num}是新数字.
Where 'new_file' is the new file name, 'file' the original file name, '${suf}' the file's suffix and ${num} a new number.
因此 some.log 必须成为 some.log.1 ,而 some.log.1 必须成为 some.log. 2 .使用我的代码, some.log 变为 some.log.1 ,但是 some.log.1 仍然是 some.log.1 .
So some.log must become some.log.1 and some.log.1 must become some.log.2. With my code some.log becomes some.log.1, but some.log.1 remains some.log.1.
我希望我足够清楚.我感谢任何建议(甚至不使用sed).
I hope I'm clear enough. I appreciate any advice (even not using sed).
更新:
@paxdiablo .我认为测试出了点问题.
@paxdiablo. Something went wrong testing I think.
现在我将这段代码用作测试;
Now I use this piece of code as test;
#!/usr/bin/bash
shft() {
for suff in {6..1} ; do
if [[ -f "$1.${suff}" ]] ; then
((nxt = suff + 1))
echo Moving "$1.${suff}" to "$1.${nxt}"
mv -f "$1.${suff}" "$1.${nxt}"
fi
done
echo Moving "$1" to "$1.1"
mv -f "$1" "$1.1"
}
clear
folder=~/logs/*.log
for i in {1..20}; do
echo ${i}> ~/logs/some.log
for fspec in ${folder} ; do
shft "${fspec}"
done
done
现在一切正常.抱歉造成混乱.
Every thing works fine now. Sorry for the confusion.
推荐答案
如果您要翻转日志文件,并根据需要获取的复杂程度,在此之前,我使用了以下部分:
If you're looking to roll over log files, and depending on how complex you need to get, I've used the following segment before:
#!/usr/bin/bash
# rollover.sh
# Rolls over log files in the current directory.
# *.log.8 -> *.log.9
# *.log.7 -> *.log.8
# : : :
# *.log.1 -> *.log.2
# *.log -> *.log.1
shft() {
# Change this '8' to one less than your desired maximum rollover file.
# Must be in reverse order for renames to work (n..1, not 1..n).
for suff in {8..1} ; do
if [[ -f "$1.${suff}" ]] ; then
((nxt = suff + 1))
echo Moving "$1.${suff}" to "$1.${nxt}"
mv -f "$1.${suff}" "$1.${nxt}"
fi
done
echo Moving "$1" to "$1.1"
mv -f "$1" "$1.1"
}
for fspec in *.log ; do
shft "${fspec}"
#date >"${fspec}" #DEBUG code
done
这将自动将日志文件滚动到9版以上,尽管您可以更改suff
for
循环以允许更多内容.
This will automatically roll over log files up to version 9 although you can just change the suff
for
loop to allow more.
添加了DEBUG
,以便自动创建新文件进行测试,下面的记录显示了它的作用:
With that DEBUG
added so new files are created automatically for testing, the following transcript shows it in action:
pax> touch qq.log ; ./rollover.sh
Moving "qq.log" to "qq.log.1"
pax> touch "has spaces.log" ; ./rollover.sh
Moving "has spaces.log" to "has spaces.log.1"
Moving "qq.log.1" to "qq.log.2"
Moving "qq.log" to "qq.log.1"
pax> ll *log*
-rw-r--r-- 1 pax None 30 2010-09-11 20:39 has spaces.log
-rw-r--r-- 1 pax None 0 2010-09-11 20:39 has spaces.log.1
-rw-r--r-- 1 pax None 30 2010-09-11 20:39 qq.log
-rw-r--r-- 1 pax None 30 2010-09-11 20:38 qq.log.1
-rw-r--r-- 1 pax None 0 2010-09-11 20:38 qq.log.2
此脚本的优点是可以很容易地对其进行配置,以处理大量的历史记录(通过更改{8..1}
位),处理带有空格的名称以及在日志文件丢失的情况下相对可靠地处理间隙.
The good thing about this script is that it's easily configurable to handle a large amount of history (by changing the {8..1}
bit), handles names with spaces, and handles gaps relatively robustly if log files go missing.
这篇关于更改文件名后缀(使用sed吗?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!