如何在命令行上找到两个或多个字符串的交集
我经常发现自己正在重命名.flac-files,以删除大量信息。我写一个循环,通过手动写出要删除的零件来编辑文件名。例如,我有一个装满这样的文件的文件夹:
$ ls -l
-rw-r--r-- 1 me me 315416376 Jan 6 2021 'Föllakzoid - 01 - I.flac'
-rw-r--r-- 1 me me 250380814 Jan 6 2021 'Föllakzoid - 02 - II.flac'
-rw-r--r-- 1 me me 336071410 Jan 6 2021 'Föllakzoid - 03 - III.flac'
-rw-r--r-- 1 me me 258787367 Jan 6 2021 'Föllakzoid - 04 - IIII.flac'
所需的输出为:
$ ls -l
-rw-r--r-- 1 me me 315416376 Jan 6 2021 '01 - I.flac'
-rw-r--r-- 1 me me 250380814 Jan 6 2021 '02 - II.flac'
-rw-r--r-- 1 me me 336071410 Jan 6 2021 '03 - III.flac'
-rw-r--r-- 1 me me 258787367 Jan 6 2021 '04 - IIII.flac'
在这里,我想删除所有文件中的“föllakzoid - ” - 部分(因为我通过文件夹结构进一步传达信息)。 而不是手动键入,
$ for file in *.flac; do a=`echo "$file" | sed 's/Föllakzoid - //'`; mv "$file" "$a"; done
我宁愿找到“föllakzoid - ” - 自动部分并将其从所有文件中删除。我可以将其抽象成一个脚本并一直重复使用(从而改善了我的bash技能)。
我在这里发现了一些类似的问题(在Stackoverflow上),但它们处理了订单并不重要的项目列表。就我而言,我想在多个较长的字符串中搜索平等的子字符串:我正在寻找一个返回多个字符串相交的函数。因此,字母的顺序很重要(与清单交流有关的其他问题相反。
/usr /bin的财富中是否有一个简单的工具?
I frequently find myself renaming .flac-files in order to remove abundant information. I write a loop where I edit filenames by manually writing out the part that I want removed. E.g. I have a folder full of files like this:
$ ls -l
-rw-r--r-- 1 me me 315416376 Jan 6 2021 'Föllakzoid - 01 - I.flac'
-rw-r--r-- 1 me me 250380814 Jan 6 2021 'Föllakzoid - 02 - II.flac'
-rw-r--r-- 1 me me 336071410 Jan 6 2021 'Föllakzoid - 03 - III.flac'
-rw-r--r-- 1 me me 258787367 Jan 6 2021 'Föllakzoid - 04 - IIII.flac'
With the desired output being:
$ ls -l
-rw-r--r-- 1 me me 315416376 Jan 6 2021 '01 - I.flac'
-rw-r--r-- 1 me me 250380814 Jan 6 2021 '02 - II.flac'
-rw-r--r-- 1 me me 336071410 Jan 6 2021 '03 - III.flac'
-rw-r--r-- 1 me me 258787367 Jan 6 2021 '04 - IIII.flac'
Here I want to remove the "Föllakzoid - "-part from all of the files (as I that information is conveyed through a folder structure further up).
Instead of manually typing
$ for file in *.flac; do a=`echo "$file" | sed 's/Föllakzoid - //'`; mv "$file" "$a"; done
I'd rather find the "Föllakzoid -"-part automatically and remove it from all files. Than I could abstract it into a script and re-use all the time (Thereby improving my bash-skills along the way).
I found some similar questions here (on stackoverflow) but they deal with lists of items where order is not important. In my case I want to search for equal substrings in multiple longer strings: I am looking for a function that returns the intersection of multiple strings. So the order of letters is important (as opposed to other questions about list-intersections.
Is there a simple tool in the riches of the depths of /usr/bin ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能想查看
重命名
。它不是标准实用程序,但在Linux上已广泛使用。也就是说,您还可以编写自己的功能/脚本,该函数/脚本与字面的地球仪一起使用(虽然您会失去
remane
的速度):代码是在jiffy中制作的 同时写一些通用,灵活和坚固的东西并不容易)
边境案例需要解决( 使用BASH参数扩展
$ {var/glob/repled}
,将第一次出现glob
用repled
替换为,但还有其他一些这可能证明有用;例如,有一个$ {var#glob}
从glob
的最短左侧匹配中,从$ var
(即,如果> $ var
将扩展到föllakzoid  -   01  -   i.flac
然后$ {var#*  -  }
将展开至01  -   i.flac
)。您可以使用这样的函数:
并执行命令:
You might want to have a look at
rename
. It's not a standard utility but it's widely available on Linux.That said, you can also write your own function/script that works with literal globs (you'll loose the speed of
remane
though):The code was made in a jiffy so there're border cases that need to be addressed (it's not so easy to write something generic, flexible and robust at the same time)
note: Here I used the bash parameter expansion
${var/glob/repl}
that replaces the first occurrence ofglob
withrepl
, but there exists a few others that might prove useful; for example there's a${var#glob}
that strips the shortest left-side match ofglob
from$var
(i.e. if$var
expands toFöllakzoid - 01 - I.flac
then${var#* - }
will expand to01 - I.flac
).Then you can use the function like this:
And that'll execute the commands:
renameutils
软件包的QMV
带有文件范围的命令将您将您置于编辑器中,以使您想要的模式更改,并将使您在退出时要求更改。对于用例不一致且需要大量特殊外壳的情况,有益。
可能不是最好的选择,但仍然是一个很好的工具。
The
renameutils
package'sqmv
command with a file glob will drop you into an editor to make the pattern change you want and will make the change you requested on exit.Good for the case where the use cases aren't 100% consistent and require a lot of special casing.
May not be the best option, but still a good tool to have in one's grasp.