【发布时间】:2019-06-24 12:06:27
【问题描述】:
在我的脑海里,我有:
阅读“原始文件”,
将第 3 行 "ENTRY1" 更改为 data_file 中第一个单词的行。
写出new_file1。
阅读“原始文件”,
将第 3 行 "ENTRY1" 更改为 data_file 中第二个单词的行。
写出new_file2
重复整个data_file。
摘录/示例:
original_file:
line1 {
line2 "id": "b5902627-0ba0-40b6-8127-834a3ddd6c2c",
line3 "name": "ENTRY1",
line4 "auto": true,
line5 "contexts": [],
line6 "responses": [
line7 {
------------
data_file:(simply a word/number List)
line1 AAA11
line2 BBB12
line3 CCC13
..100lines/Words..
-------------
*the First output/finished file would look like:
newfile1:
line1 {
line2 "id": "b5902627-0ba0-40b6-8127-834a3ddd6c2c",
line3 "name": "AAA11",
line4 "auto": true,
line5 "contexts": [],
line6 "responses": [
line7 {
------------
and the Second:
newfile2:
line1 {
line2 "id": "b5902627-0ba0-40b6-8127-834a3ddd6c2c",
line3 "name": "BBB12",
line4 "auto": true,
line5 "contexts": [],
line6 "responses": [
line7 {
------------
..等等。
我一直在尝试使用 sed,类似
awk 'FNR==$n1{if((getline line < "data_file") > 0) fprint '/"id:"/' '/""/' line ; next}$n2' < newfile
and.. 作为 shell 脚本的开始..
#!/bin/bash
n1=3
n2=2
sed '$n1;$n2 data_file' original_file > newfile
任何帮助将不胜感激.. 我一直在尝试将 SO.. 上发现的各种技术结合在一起.. 一次一件事.. 学习如何替换..
然后从第二个文件替换..但它超出了我的知识范围。再次感谢。
我的数据文件中有大约31,000 LINES .. 所以这是必要的..
(自动化)。它是一次性的,但可能对其他人非常有用?
【问题讨论】:
-
这是您试图操纵的 JSON 内容吗?不要使用
awk或正则表达式工具,而是使用语法感知解析器jq -
是的。这是一个 JSON 文件。谢谢。我从来没有听说过 jq .. 只是在 github 上找到它。 :) 我会继续努力的。
-
python中的快速解决方案!
-
将 data_file 添加到您的问题中。
-
仅供参考:订单和 json 文件 -> stackoverflow.com/questions/16870416/… 空白和 json 文件 -> stackoverflow.com/questions/4150621/…