如何在文本文件中编写脚本 copy/paste

how to script copy/paste within text file

我如何创建 macro/script 将我需要的代码信息放入以下语法中

switch n

case 1

    country=af
    coutry_word=Afghanistan
    country_flag=https://imgur.com/yRbLG3B
    country_code_number=+324
    
    break;

case 2
    ...

我在 notepad++ 中执行此操作,除了为所有这些变量添加唯一信息外,我能够自动创建所有内容。 (这些 case -s 有 240 个,所以这意味着我必须手动将 960 次放入这些变量中的每一个)

但是我已经有了需要放入变量的顺序信息,并且(为了更容易编写脚本)将这些信息放入与此代码相同的文件中。我只需要相应地添加它们(并且列表是排序的,所以它只需要按照排序的方式放入变量)

这就是我的意思

    https://imgur.com/y9G1Nim
    https://imgur.com/xmU8rwh
    https://imgur.com/wfcLo9a
    https://imgur.com/wdRUl1Z
    https://imgur.com/vxnPnl3
    https://imgur.com/uNhaTYi
...

Afghanistan 
Åland Islands
Algeria 
Andorra
Antarctica
Argentina
Australia
...

TPE
AFG
ALB
ALG
ASA
AND
ANG
AIA
ROS
ATG
ARG
...

+123
+458
+554
+588
+585
+584
...

它们与这段代码在同一个文件中 在记事本++中使用宏

我只得到这么多(使用 NP++ 宏)

switch n
    
    case 1
    
        country=
        coutry_word=
        country_flag=
        country_code_number=
        
        break;
    
    case 2
        ...

所以剩下的就是将这些有序的文本放入这些变量中。我尝试使用宏,但它不会继续到文件末尾。

如果 NP++ 做不到,我也很乐意 Vim

在我的类 Unix 系统上 Vim,我将如何解决这个问题:

  1. 清理数据以便没有前导或尾随空格:

    :%left
    :keeppattern [range]s/\s\+$//
    
  2. Select每个数据块并将其写入自己的文件,加上适当的前缀:

    1. 将光标放在“国家标志”块上。
    2. 在视觉上 select 带有 vip 的块。
    3. :'<,'>!sed 's/^/country_flag=/' >> country_flag.txt.
    4. 将块写入文件

    对相应文件中的其他块重复:

    country_word.txt
    country.txt
    country_code_number.txt
    

    此时,我们应该有以下内容:

    $ cat country.txt
    country=TPE
    country=AFG
    country=ALB
    country=ALG
    country=ASA
    country=AND
    
    $ cat country_code_number.txt
    country_code_number=+123
    country_code_number=+458
    country_code_number=+554
    country_code_number=+588
    country_code_number=+585
    country_code_number=+584
    
    $ cat country_flag.txt
    country_flag=https://imgur.com/y9G1Nim
    country_flag=https://imgur.com/xmU8rwh
    country_flag=https://imgur.com/wfcLo9a
    country_flag=https://imgur.com/wdRUl1Z
    country_flag=https://imgur.com/vxnPnl3
    country_flag=https://imgur.com/uNhaTYi
    
    $ cat country_word.txt
    country_word=Afghanistan
    country_word=Åland Islands
    country_word=Algeria
    country_word=Andorra
    country_word=Antarctica
    country_word=Argentina
    
  3. 交错那些文件并将结果插入缓冲区:

    :read !paste -d '\n' country*
    

    应该给我们以下内容:

    country=TPE
    country_code_number=+123
    country_flag=https://imgur.com/y9G1Nim
    country_word=Afghanistan
    country=AFG
    country_code_number=+458
    country_flag=https://imgur.com/xmU8rwh
    country_word=Åland Islands
    country=ALB
    country_code_number=+554
    country_flag=https://imgur.com/wfcLo9a
    country_word=Algeria
    country=ALG
    country_code_number=+588
    country_flag=https://imgur.com/wdRUl1Z
    country_word=Andorra
    country=ASA
    country_code_number=+585
    country_flag=https://imgur.com/vxnPnl3
    country_word=Antarctica
    country=AND
    country_code_number=+584
    country_flag=https://imgur.com/uNhaTYi
    country_word=Argentina
    
  4. 剩下的就是把所有这些变成case块。

    我们可以尝试使用漂亮的单行代码来完成,但将任务拆分为更简单的子任务会更容易且更省时。

    1. 在每个块上方添加case n以及一个空行:

      :let i = 1 | g/country=/put!=['','case '.i] | let i += 1
      

      这给了我们以下内容:

      case 1
      country=TPE
      country_code_number=+123
      country_flag=https://imgur.com/y9G1Nim
      country_word=Afghanistan
      
      case 2
      country=AFG
      country_code_number=+458
      country_flag=https://imgur.com/xmU8rwh
      country_word=Åland Islands
      
      case 3
      country=ALB
      country_code_number=+554
      country_flag=https://imgur.com/wfcLo9a
      country_word=Algeria
      
      case 4
      country=ALG
      country_code_number=+588
      country_flag=https://imgur.com/wdRUl1Z
      country_word=Andorra
      
      case 5
      country=ASA
      country_code_number=+585
      country_flag=https://imgur.com/vxnPnl3
      country_word=Antarctica
      
      case 6
      country=AND
      country_code_number=+584
      country_flag=https://imgur.com/uNhaTYi
      country_word=Argentina
      
    2. 在每个块的末尾添加break语句:

      :g/country_word/put='break;'
      

      这给了我们以下内容:

      case 1
      country=TPE
      country_code_number=+123
      country_flag=https://imgur.com/y9G1Nim
      country_word=Afghanistan
      break;
      
      case 2
      country=AFG
      country_code_number=+458
      country_flag=https://imgur.com/xmU8rwh
      country_word=Åland Islands
      break;
      
      case 3
      country=ALB
      country_code_number=+554
      country_flag=https://imgur.com/wfcLo9a
      country_word=Algeria
      break;
      
      case 4
      country=ALG
      country_code_number=+588
      country_flag=https://imgur.com/wdRUl1Z
      country_word=Andorra
      break;
      
      case 5
      country=ASA
      country_code_number=+585
      country_flag=https://imgur.com/vxnPnl3
      country_word=Antarctica
      break;
      
      case 6
      country=AND
      country_code_number=+584
      country_flag=https://imgur.com/uNhaTYi
      country_word=Argentina
      break;
      
    3. 最后一步,缩进 country* 行:

      :g/^country/>
      

      应该给我们以下内容:

      case 1
        country=TPE
        country_code_number=+123
        country_flag=https://imgur.com/y9G1Nim
        country_word=Afghanistan
      break;
      
      case 2
        country=AFG
        country_code_number=+458
        country_flag=https://imgur.com/xmU8rwh
        country_word=Åland Islands
      break;
      
      case 3
        country=ALB
        country_code_number=+554
        country_flag=https://imgur.com/wfcLo9a
        country_word=Algeria
      break;
      
      case 4
        country=ALG
        country_code_number=+588
        country_flag=https://imgur.com/wdRUl1Z
        country_word=Andorra
      break;
      
      case 5
        country=ASA
        country_code_number=+585
        country_flag=https://imgur.com/vxnPnl3
        country_word=Antarctica
      break;
      
      case 6
        country=AND
        country_code_number=+584
        country_flag=https://imgur.com/uNhaTYi
        country_word=Argentina
      break;
      

所有命令加在一起:

:left
:keepp [range]s/\s\+$
vip
:'<,'>!sed 's/^/country_flag=/' >> country_flag.txt
(move cursor to next block)
vip
:<Up>
<C-f>
s/flag/word/g
<CR>
(move cursor to next block)
vip
:<Up>
<C-f>
s/_word//g
<CR>
(move cursor to next block)
vip
:<Up>
<C-f>
s/country/&_code_number/g
<CR>
:read !paste -d '\n' country*
:let i = 1 | g/country=/put!=['','case '.i] | let i += 1
:g/country_word/put='break;'
:g/^country/>

现在,我 2009 年的非 vimmer 自我可能会认为这太奇怪或太复杂了,但是,对于我 2022 年初的 vimmer 自我来说,嗯……一切都很自然(除了 paste 语法,我总是要查找)所以我想这完全是一个人使用工具的经验问题。对我来说,验证我的命令结果并写下答案所花的时间比在脑海中形成答案所花的时间要长得多,或者比不必同时写下答案而实际去做所花的时间要长得多。我们正在谈论两三分钟的事情,tops。

参考:

:help :left
:help :keeppattern
:help ip
:help :range
:help :!
:help command-line-window
:help :read
:help :let
:help :global
:help :put
:help :>
$ man sed
$ man paste