使用bash将第二行的内容从一个文档复制到另一文档的第二行的开头

时间:2019-02-26 01:29:36

标签: bash

我正在尝试将一个文档的第二行复制到另一文档的第二行的开头。有点像这样:

Document 3
a
2b
c
4d
e
6f

成为:

sed -n '2~2p' document1.txt

实际文档中的数据更多。到目前为止,我能够从文档1中导出第二行:

using System;
using System.Text;
// Reference assembly 'System.Security'
using System.Security.Cryptography;

namespace TestProtectedData
{
    class Program
    {
        // Encrypt plainText and return a base-64 encoded cipher text
        static string Encrypt(string plainText)
        {
            byte[] plainBytes = UnicodeEncoding.UTF8.GetBytes(plainText);
            byte[] cipherBytes = ProtectedData.Protect(plainBytes, null, DataProtectionScope.CurrentUser);
            return Convert.ToBase64String(cipherBytes);
        }

        // Decrypt a base-64 encoded cipher text and return plain text
        static string Decrypt(string cipherBase64)
        {
            var cipherBytes = Convert.FromBase64String(cipherBase64);
            var plainBytes = ProtectedData.Unprotect(cipherBytes, null, DataProtectionScope.CurrentUser);
            return Encoding.UTF8.GetString(plainBytes, 0, plainBytes.Length);
        }

        static void Main(string[] args)
        {
            // plainTextToEncrypt can be a connection string, user credentials or similar
            var plainTextToEncrypt = "Hello, secret!";
            Console.WriteLine("Plain text: " + plainTextToEncrypt);
            // Getting a base64 encoded string as the encryption result for easy storage
            var cipherBase64 = Encrypt(plainTextToEncrypt);
            // Save the cipherBase64 string into a configuration file or similar
            Console.WriteLine("Encrypted text (base64): " + cipherBase64);
            // When needed, read the cipherBase64 string and decrypt the text
            var plainTextDecrypted = Decrypt(cipherBase64);
            Console.WriteLine("Decrypted text: " + plainTextDecrypted);
            Console.ReadKey();
        }
    }
}

但是我不知道如何将其复制到文档2的第二行的开头。有人可以帮助我实现这一目标吗? 谢谢

3 个答案:

答案 0 :(得分:1)

给出f1是:

client.on("guildMemberAdd", (guild) => {
    guild.owner.send("Hello, thanks for adding me to " + guild.name + "!");
}

给出f2是:

1
2
3
4
5
6

让f1中的奇数行空白:

a
b
c
d
e
f

现在f1a是:

sed '1~2s/^./ /' f1 > f1a

现在,我们非常老却被遗忘的朋友. 2 4 6

paste给出:

paste -d':' f1a f2

取出空格/定界符:

 :a
2:b
 :c
4:d
 :e
6:f

答案 1 :(得分:1)

TenG解决方案的更便携式版本:

sed <f1 'g;n' | paste -d '\0' - f2
  • g-用(空)保留替换模式空间
  • n-打印图案空间,然后替换为下一行并隐式打印
  • -d '\0'-粘贴使用\0来表示“不要插入定界符”,而不是 NUL

答案 2 :(得分:0)

一个简短的awk程序:

awk '
    NR == FNR    {if (NR % 2 == 0) d1[FNR] = $0; next} 
    FNR % 2 == 0 {$0 = d1[FNR] $0} 
    1
' document1.txt document2.txt