简体   繁体   English

我如何抓取文本并将它们按特定顺序放置?

[英]How do i grab text and place them in a particular order?

I am using a google script ( js ) to rename files in my google drive where i input oldname and newname separated by commas.我正在使用谷歌脚本( js )重命名我的谷歌驱动器中的文件,我在其中输入旧名称和新名称,用逗号分隔。 The line where input old and new name goes like this -输入新旧名称的行是这样的 -

function rename(iA=['old1.mp4','old2.mp4','old3.mp4'],oA=['new1.mp4','new2.mp4','new3.mp4']) {

This is quite self explanatory, the old1.mp4 will get renamed to new1.mp4, old2.mp4 to new2.mp4 and so on这很不言自明,old1.mp4 将重命名为 new1.mp4,old2.mp4 将重命名为 new2.mp4,依此类推

The script works great no issues, the problem is i have like thousands of file to rename and can't enter each manually in script.该脚本运行良好,没有问题,问题是我有数千个文件要重命名,并且无法在脚本中手动输入每个文件。 Luckily i have them present in a rename.txt tile from where i would like to grab them to get placed correctly in my script/function幸运的是,我将它们显示在 rename.txt 磁贴中,我想从该磁贴中抓取它们以正确放置在我的脚本/函数中

Format of text in rename.txt is OldName NewName rename.txt 中的文本格式为OldName NewName

The content of rename.txt file is like this.. rename.txt 文件的内容是这样的..

RandomAlphanumericChars.mp4 Lecture 1 - Some topic.mp4
RandomAlphanumericChars.mp4 Lecture 2 - Some topic.mp4
RandomAlphanumericChars.mp4 Lecture 3 - Some topic.mp4

The 2 differentiating apparent pattern here..这里有两种不同的明显模式..

1) The first time a space occurs in any line, the oldname has ended. 1) 任何一行中第一次出现空格时,旧名称已结束。 2) The newname string always begins with word Lecture. 2) newname 字符串总是以单词 Lecture 开头。

( PS don't use.mp4 ending to recognise end of oldname as most of oldnames have.mp4 multiple times in their name, it will cause issues. example of one oldname - rgGW6m9j-32313921.mp4-rgGW6m9j-32313921.mp4.mp4 ) (PS不要使用.mp4结尾来识别旧名称的结尾,因为大多数旧名称的名称中有多次.mp4,这会导致问题。一个旧名称的示例 - rgGW6m9j-32313921.mp4-rgGW6m9j-32313921.mp4.mp4 )

To put everything in final perspective, the regex should grab text from rename.txt and place in the function like this..为了把所有东西放在最后,正则表达式应该从 rename.txt 中获取文本并像这样放在 function 中。

function rename(iA=['RandomAlphanumericChars.mp4','RandomAlphanumericChars.mp4','RandomAlphanumericChars.mp4'],oA=['Lecture 1 - SomeTopic.mp4','Lecture 2 - Some topic.mp4','Lecture 3 - Some topic.mp4']) {

Only thing important here is that the order remains correct ie the oldName and NewName should be placed in function in the same order as it is in the rename.txt file这里唯一重要的是顺序保持正确,即 oldName 和 NewName 应该以与 rename.txt 文件中相同的顺序放置在 function 中

Here are some basic constructs and hard part done, you need to add the missing text这是一些基本的结构和困难的部分,您需要添加缺少的文本

$ awk -v q="'" 'function join(a,s) 
                     {t=""; for(k in a) {s=s t q a[k] q; t=","} return s}
                function wrap(x) 
                     {return "[" x "]"}

                {a[NR]=$1; $1=""; sub(/^ /,""); b[NR]=$0}
            END {print "iA=" wrap(join(a)) "," "oA=" wrap(join(b))}' file

iA=['RandomAlphanumericChars.mp4','RandomAlphanumericChars.mp4','RandomAlphanumericChars.mp4'],oA=['Lecture 1 - Some topic.mp4','Lecture 2 - Some topic.mp4','Lecture 3 - Some topic.mp4']

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

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