[英]sed file remove leading text
I've been trying to get this working for a bit now... this question has helped, but I'm still struggling to get this to work. 我一直在尝试使它工作一些…… 这个问题有所帮助,但我仍在努力使其工作。 I'd prefer not to install homebrew, because this is a rare task that I'm performing right now.
我不想安装homebrew,因为这是我现在正在执行的罕见任务。
I have a couple thousand files with a string of text, an underscore, more text, underscore and finally the important file name that I want to preserve. 我有数千个文件,其中包含文本字符串,下划线,更多文本,下划线,最后是我要保留的重要文件名。 Dropping the first
*_*_
and preserving the last part, with file extension, is attempted with \\(*\\)/\\1/
. 尝试使用
\\(*\\)/\\1/
删除前一个*_*_
并保留最后一个扩展名(具有文件扩展名\\(*\\)/\\1/
。
I've tried a couple different things and all I've got is the original filenames being spit out again. 我已经尝试了几种不同的方法,但我得到的只是原始文件名再次被吐出。 Any help is appreciated.
任何帮助表示赞赏。 - not sure if it's a regex issue, or sed, or probably a little of both.
-不知道这是正则表达式问题,还是sed,或者两者都有。
ls | sed 's/^*_*_\(*\)/\1/' > ouput.txt;
ls | sed 's/^*_*_\(*\$\)/\1/' > out.txt
ls | sed 's/\(^*_*_\)\(*\$\)/\2/' > out.txt
ls | sed 's/\(^.*_+.*_+\)\(.*\$\)/mv & \2/' > out.txt
Does this regex do the trick? 这个正则表达式可以解决问题吗? If not, please report how its output is off (details) and I'll help you tune it.
如果不是,请报告其输出如何关闭(详细信息),我将帮助您进行调整。
ls -1 | sed -e 's/^[^_]*_[^_]*_//'
Note 1: You may want to use ls -1
to format the files into a single column. 注意1:您可能希望使用
ls -1
将文件格式化为单列。
Note 2: The approach above simply removes the unwanted part of your file names, rather than trying to store in a regex buffer the part that you do want. 注意2:以上方法只是删除了文件名中不需要的部分,而不是尝试将所需的部分存储在正则表达式缓冲区中。
EDIT 编辑
And here's a bash script that performs the rename. 这是一个执行重命名的bash脚本。
for f in `ls -1`
do
new_name=`echo "$f" | sed 's/^[^_]*_[^_]*_//'`
mv "$f" "$new_name"
done
Can be written as a one-liner, but I went for clarity over brevity. 可以写成一线,但为了简洁起见,我还是写清楚了。
command | sed 's;^.*_;;'
will do the trick. 会成功的 Use
find
command instead of ls
. 使用
find
命令代替ls
。
For example, 例如,
find . -type f | sed 's;^.*_;;'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.