在一个字符串的前面和另一个字符串的末尾以交替行添加一个字符串

Posted

技术标签:

【中文标题】在一个字符串的前面和另一个字符串的末尾以交替行添加一个字符串【英文标题】:Add a string in front of a string and at the end of another string in alternating lines 【发布时间】:2021-09-06 08:07:00 【问题描述】:

这是我的文件格式:

"index":
"LAST_MODIFIED_BY":"john","CREATED_BY":"david","LAST_MODIFIED_DATE":"2018-01-11T06:39:11.000Z","CREATION_DATE":"2018-01-11T06:39:11.000Z","INGESTION_DATE":"2018-01-11T06:39:11.000Z","DECISION":"AGREE","BATCH_TAG":"","TASK_TYPE_NAME":"Test","PUBLISHER_ID":"abc123","ID":"700adca2-c4d4-4245-8174-0479f7123a63"
"index":
"LAST_MODIFIED_BY":"joe","CREATED_BY":"brian","LAST_MODIFIED_DATE":"2018-01-11T06:39:11.000Z","CREATION_DATE":"2018-01-11T06:39:11.000Z","INGESTION_DATE":"2018-01-11T06:39:11.000Z","DECISION":"AGREE","BATCH_TAG":"","TASK_TYPE_NAME":"Test2","PUBLISHER_ID":"abc123","ID":"700adca2-c4d4-4245-8174-0479f7123a62"
...
"index":
"LAST_MODIFIED_BY":"sarah","CREATED_BY":"hannah","LAST_MODIFIED_DATE":"2018-01-11T06:39:11.000Z","CREATION_DATE":"2018-01-11T06:39:11.000Z","INGESTION_DATE":"2018-01-11T06:39:11.000Z","DECISION":"AGREE","BATCH_TAG":"","TASK_TYPE_NAME":"Test8","PUBLISHER_ID":"abc123","ID":"700adca2-c4d4-4245-8174-0479f7123a61"

我需要添加"TAGS": 来包含这两个字段:BATCH_TAGTASK_TYPE_NAME,因此所需的输出如下所示:

"index":
"LAST_MODIFIED_BY":"john","CREATED_BY":"david","LAST_MODIFIED_DATE":"2018-01-11T06:39:11.000Z","CREATION_DATE":"2018-01-11T06:39:11.000Z","INGESTION_DATE":"2018-01-11T06:39:11.000Z","DECISION":"AGREE","TAGS":"BATCH_TAG":"","TASK_TYPE_NAME":"Test","PUBLISHER_ID":"abc123","ID":"700adca2-c4d4-4245-8174-0479f7123a63"
"index":
"LAST_MODIFIED_BY":"joe","CREATED_BY":"brian","LAST_MODIFIED_DATE":"2018-01-11T06:39:11.000Z","CREATION_DATE":"2018-01-11T06:39:11.000Z","INGESTION_DATE":"2018-01-11T06:39:11.000Z","DECISION":"AGREE","TAGS":"BATCH_TAG":"","TASK_TYPE_NAME":"Test2","PUBLISHER_ID":"abc123","ID":"700adca2-c4d4-4245-8174-0479f7123a62"
...
"index":
"LAST_MODIFIED_BY":"sarah","CREATED_BY":"hannah","LAST_MODIFIED_DATE":"2018-01-11T06:39:11.000Z","CREATION_DATE":"2018-01-11T06:39:11.000Z","INGESTION_DATE":"2018-01-11T06:39:11.000Z","DECISION":"AGREE","TAGS":"BATCH_TAG":"","TASK_TYPE_NAME":"Test8","PUBLISHER_ID":"abc123","ID":"700adca2-c4d4-4245-8174-0479f7123a61"

我能找到的最接近的是这个:Add a prefix string to beginning of each line,但它在每一行的前面添加了一个字符串,这并不是我真正想要的。

请问有人能解释一下吗?

谢谢!

【问题讨论】:

【参考方案1】:

这里的棘手之处在于确保将新的 TAGS 密钥插入所需的位置。使用 jq,这可以通过 with_entries 来完成:

if has("BATCH_TAG")
then .BATCH_TAG = BATCH_TAG, TASK_TYPE_NAME
| with_entries(.key |= if . == "BATCH_TAG" 
                       then "TAGS"
                       else . end)
| del(.TASK_TYPE_NAME)
else .
end

【讨论】:

感谢您的回复,但如何使用jq 运行它?我尝试运行jq if has("BATCH_TAG") then .BATCH_TAG = BATCH_TAG, TASK_TYPE_NAME | with_entries(.key |= if . == "BATCH_TAG" then "TAGS" else . end) | del(.TASK_TYPE_NAME) else . end input.txt > output.txt,但没有成功。 也许你会发现将 jq 程序放在一个文件中最简单的方法,比如 program.jq,然后运行类似:jq -cf program.jq input.json 非常感谢,这成功了!【参考方案2】:

由于您的问题是Could anyone shed any light onto this please?,这里有一些亮点:

您正在使用的文件格式似乎是JSON,因此请使用能够理解JSON 的工具对其进行编辑,例如jq.

例如,在您的示例输入上运行时(在将其弄乱并使其无法测试的 ... 行被删除后)这里是 jq 漂亮打印它:

$ cat file
"index":
"LAST_MODIFIED_BY":"john","CREATED_BY":"david","LAST_MODIFIED_DATE":"2018-01-11T06:39:11.000Z","CREATION_DATE":"2018-01-11T06:39:11.000Z","INGESTION_DATE":"2018-01-11T06:39:11.000Z","DECISION":"AGREE","BATCH_TAG":"","TASK_TYPE_NAME":"Test","PUBLISHER_ID":"abc123","ID":"700adca2-c4d4-4245-8174-0479f7123a63"
"index":
"LAST_MODIFIED_BY":"joe","CREATED_BY":"brian","LAST_MODIFIED_DATE":"2018-01-11T06:39:11.000Z","CREATION_DATE":"2018-01-11T06:39:11.000Z","INGESTION_DATE":"2018-01-11T06:39:11.000Z","DECISION":"AGREE","BATCH_TAG":"","TASK_TYPE_NAME":"Test2","PUBLISHER_ID":"abc123","ID":"700adca2-c4d4-4245-8174-0479f7123a62"
"index":
"LAST_MODIFIED_BY":"sarah","CREATED_BY":"hannah","LAST_MODIFIED_DATE":"2018-01-11T06:39:11.000Z","CREATION_DATE":"2018-01-11T06:39:11.000Z","INGESTION_DATE":"2018-01-11T06:39:11.000Z","DECISION":"AGREE","BATCH_TAG":"","TASK_TYPE_NAME":"Test8","PUBLISHER_ID":"abc123","ID":"700adca2-c4d4-4245-8174-0479f7123a61"

$ jq . < file

  "index": 


  "LAST_MODIFIED_BY": "john",
  "CREATED_BY": "david",
  "LAST_MODIFIED_DATE": "2018-01-11T06:39:11.000Z",
  "CREATION_DATE": "2018-01-11T06:39:11.000Z",
  "INGESTION_DATE": "2018-01-11T06:39:11.000Z",
  "DECISION": "AGREE",
  "BATCH_TAG": "",
  "TASK_TYPE_NAME": "Test",
  "PUBLISHER_ID": "abc123",
  "ID": "700adca2-c4d4-4245-8174-0479f7123a63"


  "index": 


  "LAST_MODIFIED_BY": "joe",
  "CREATED_BY": "brian",
  "LAST_MODIFIED_DATE": "2018-01-11T06:39:11.000Z",
  "CREATION_DATE": "2018-01-11T06:39:11.000Z",
  "INGESTION_DATE": "2018-01-11T06:39:11.000Z",
  "DECISION": "AGREE",
  "BATCH_TAG": "",
  "TASK_TYPE_NAME": "Test2",
  "PUBLISHER_ID": "abc123",
  "ID": "700adca2-c4d4-4245-8174-0479f7123a62"


  "index": 


  "LAST_MODIFIED_BY": "sarah",
  "CREATED_BY": "hannah",
  "LAST_MODIFIED_DATE": "2018-01-11T06:39:11.000Z",
  "CREATION_DATE": "2018-01-11T06:39:11.000Z",
  "INGESTION_DATE": "2018-01-11T06:39:11.000Z",
  "DECISION": "AGREE",
  "BATCH_TAG": "",
  "TASK_TYPE_NAME": "Test8",
  "PUBLISHER_ID": "abc123",
  "ID": "700adca2-c4d4-4245-8174-0479f7123a61"

还有你想要的输出:

$ cat file2
"index":
"LAST_MODIFIED_BY":"john","CREATED_BY":"david","LAST_MODIFIED_DATE":"2018-01-11T06:39:11.000Z","CREATION_DATE":"2018-01-11T06:39:11.000Z","INGESTION_DATE":"2018-01-11T06:39:11.000Z","DECISION":"AGREE","TAGS":"BATCH_TAG":"","TASK_TYPE_NAME":"Test","PUBLISHER_ID":"abc123","ID":"700adca2-c4d4-4245-8174-0479f7123a63"
"index":
"LAST_MODIFIED_BY":"joe","CREATED_BY":"brian","LAST_MODIFIED_DATE":"2018-01-11T06:39:11.000Z","CREATION_DATE":"2018-01-11T06:39:11.000Z","INGESTION_DATE":"2018-01-11T06:39:11.000Z","DECISION":"AGREE","TAGS":"BATCH_TAG":"","TASK_TYPE_NAME":"Test2","PUBLISHER_ID":"abc123","ID":"700adca2-c4d4-4245-8174-0479f7123a62"
"index":
"LAST_MODIFIED_BY":"joe","CREATED_BY":"brian","LAST_MODIFIED_DATE":"2018-01-11T06:39:11.000Z","CREATION_DATE":"2018-01-11T06:39:11.000Z","INGESTION_DATE":"2018-01-11T06:39:11.000Z","DECISION":"AGREE","TAGS":"BATCH_TAG":"","TASK_TYPE_NAME":"Test2","PUBLISHER_ID":"abc123","ID":"700adca2-c4d4-4245-8174-0479f7123a62"

$ jq . < file2

  "index": 


  "LAST_MODIFIED_BY": "john",
  "CREATED_BY": "david",
  "LAST_MODIFIED_DATE": "2018-01-11T06:39:11.000Z",
  "CREATION_DATE": "2018-01-11T06:39:11.000Z",
  "INGESTION_DATE": "2018-01-11T06:39:11.000Z",
  "DECISION": "AGREE",
  "TAGS": 
    "BATCH_TAG": "",
    "TASK_TYPE_NAME": "Test"
  ,
  "PUBLISHER_ID": "abc123",
  "ID": "700adca2-c4d4-4245-8174-0479f7123a63"


  "index": 


  "LAST_MODIFIED_BY": "joe",
  "CREATED_BY": "brian",
  "LAST_MODIFIED_DATE": "2018-01-11T06:39:11.000Z",
  "CREATION_DATE": "2018-01-11T06:39:11.000Z",
  "INGESTION_DATE": "2018-01-11T06:39:11.000Z",
  "DECISION": "AGREE",
  "TAGS": 
    "BATCH_TAG": "",
    "TASK_TYPE_NAME": "Test2"
  ,
  "PUBLISHER_ID": "abc123",
  "ID": "700adca2-c4d4-4245-8174-0479f7123a62"


  "index": 


  "LAST_MODIFIED_BY": "joe",
  "CREATED_BY": "brian",
  "LAST_MODIFIED_DATE": "2018-01-11T06:39:11.000Z",
  "CREATION_DATE": "2018-01-11T06:39:11.000Z",
  "INGESTION_DATE": "2018-01-11T06:39:11.000Z",
  "DECISION": "AGREE",
  "TAGS": 
    "BATCH_TAG": "",
    "TASK_TYPE_NAME": "Test2"
  ,
  "PUBLISHER_ID": "abc123",
  "ID": "700adca2-c4d4-4245-8174-0479f7123a62"

阅读 jq 手册页和/或 google 以获取有关如何将其应用于您的问题的更多信息。

【讨论】:

感谢 Ed,我浏览了 jq 的手册页并找到了 with_entries 示例,这些示例帮助我更好地理解了这个强大的 jq 实用程序。谢谢!

以上是关于在一个字符串的前面和另一个字符串的末尾以交替行添加一个字符串的主要内容,如果未能解决你的问题,请参考以下文章

交替合并字符串

每日一题1768. 交替合并字符串

LeetCode 1768. 交替合并字符串

JQuery追加到表行的末尾而不是Tbody?

sed命令,搜索某个字符串,结果有多个匹配行,在第一个匹配行的前面添加一行内容

使用python在文本文件的每一行末尾添加一个特定的字符串(在这种情况下为“\\\hline”以准备一个乳胶表)