简体   繁体   中英

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. 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

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

Format of text in rename.txt is OldName NewName

The content of rename.txt file is like this..

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. 2) The newname string always begins with word 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 )

To put everything in final perspective, the regex should grab text from rename.txt and place in the function like this..

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

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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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