【问题标题】:sequentially replace a word using another text file list of words - data_file使用另一个文本文件的单词列表顺序替换一个单词 - data_file
【发布时间】: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/…

标签: regex awk grep cat


【解决方案1】:

假设我们正在尝试更改某些 JSON 数据中的“名称”,并且新值将是纯字母数字(以便双引号正常工作):

#!/bin/bash

n=1
cat data_file | while read value; do
    jq <original_file >"newfile$n" ".name = \"$value\""
    ((n++))
done

【讨论】:

  • 使用jq的好答案! +1
  • 感谢 jhnc,学习这个 jq 很有趣!谢谢..我也会尝试你的方法。干杯。
  • 这也很好用!我很好奇它是如何运作的。如果我可以,例如,它与 .name (变量)完美配合,但是说,如果我想改变不同的行,它读起来就像第一个但被称为“语音“:并且显然在我的 original_file 下方.. 它不会更改该行,而是将其添加到底部.. 我看不到这里的任何一个答案中的哪个位置实际上正在读取 orignal_file 的 Line#,但答案都不起作用如果更改“名称”..此刻只是一个好奇心。 :) 感谢您高效快速的脚本!干杯
  • 您确定语音没有嵌套在结构中吗?例如,响应中的某处?
  • 啊。你是对的。我没听懂..这是在回应[ ..数字。大声笑
【解决方案2】:

在 Python 2.+ 中:

输入:

more original_file.json data_file 
::::::::::::::
original_file.json
::::::::::::::
{
  "id": "b5902627-0ba0-40b6-8127-834a3ddd6c2c",
  "name": "ENTRY1",
  "auto": true,
  "contexts": [],
  "responses": []
}
::::::::::::::
data_file
::::::::::::::
AAA11
BBB12
CCC13

python 脚本:

import json

#open the original json file
with open('original_file.json') as handle:
  #create a dict based on the json content
  dictdump = json.loads(handle.read())
  #file counter
  i = 1
  #open the data file
  f = open("data_file", "r")
  #get all lines of the data file
  lines = f.read().splitlines()
  #close it
  f.close()
  #for each line of the data file
  for line in lines:
    #change the value of the json name element by the current line
    dictdump['name'] = line
    #open newfileX
    o = open("newfile" + str(i),'w')
    #dump the content of modified json
    json.dump(dictdump,o)
    #close the file
    o.close()
    #increase the counter value
    i += 1

输出:

more newfile*
::::::::::::::
newfile1
::::::::::::::
{"contexts": [], "auto": true, "responses": [], "id": "b5902627-0ba0-40b6-8127-834a3ddd6c2c", "name": "AAA11"}
::::::::::::::
newfile2
::::::::::::::
{"contexts": [], "auto": true, "responses": [], "id": "b5902627-0ba0-40b6-8127-834a3ddd6c2c", "name": "BBB12"}
::::::::::::::
newfile3
::::::::::::::
{"contexts": [], "auto": true, "responses": [], "id": "b5902627-0ba0-40b6-8127-834a3ddd6c2c", "name": "CCC13"}

如果需要垂直输出json转储,可以改行: json.dump(dictdump, o) 变成 json.dump(dictdump, o, indent=4)。这将产生:

more newfile*
::::::::::::::
newfile1
::::::::::::::
{
    "contexts": [], 
    "auto": true, 
    "responses": [], 
    "id": "b5902627-0ba0-40b6-8127-834a3ddd6c2c", 
    "name": "AAA11"
}
::::::::::::::
newfile2
::::::::::::::
{
    "contexts": [], 
    "auto": true, 
    "responses": [], 
    "id": "b5902627-0ba0-40b6-8127-834a3ddd6c2c", 
    "name": "BBB12"
}
::::::::::::::
newfile3
::::::::::::::
{
    "contexts": [], 
    "auto": true, 
    "responses": [], 
    "id": "b5902627-0ba0-40b6-8127-834a3ddd6c2c", 
    "name": "CCC13"
}

文档:https://docs.python.org/2/library/json.html

较新的版本保持与输入相同的顺序:

import json
from collections import OrderedDict

#open the original json file
with open('original_file.json') as handle:
  #create a dict based on the json content
  dictdump = json.loads(handle.read(), object_pairs_hook=OrderedDict)
  #file counter
  i = 1
  #open the data file
  f = open("data_file", "r")
  #get all lines of the data file
  lines = f.read().splitlines()
  #close it
  f.close()
  #for each line of the data file
  for line in lines:
    #change the value of the json name element by the current line
    dictdump['name'] = line
    #open newfileX
    o = open("newfile" + str(i),'w')
    #dump the content of modified json
    json.dump(dictdump, o, indent=4)
    #close the file
    o.close()
    #increase the counter value
    i += 1

输出:

more newfile*
::::::::::::::
newfile1
::::::::::::::
{
    "id": "b5902627-0ba0-40b6-8127-834a3ddd6c2c", 
    "name": "AAA11", 
    "auto": true, 
    "contexts": [], 
    "responses": []
}
::::::::::::::
newfile2
::::::::::::::
{
    "id": "b5902627-0ba0-40b6-8127-834a3ddd6c2c", 
    "name": "BBB12", 
    "auto": true, 
    "contexts": [], 
    "responses": []
}
::::::::::::::
newfile3
::::::::::::::
{
    "id": "b5902627-0ba0-40b6-8127-834a3ddd6c2c", 
    "name": "CCC13", 
    "auto": true, 
    "contexts": [], 
    "responses": []
}

【讨论】:

  • 太棒了!我马上试一试,然后告诉你,谢谢。
  • 我认为这行得通!一件事,我怎样才能让新文件输出垂直,而不是水平传播..这最终可能很重要。在我在 31,000 个文件上运行你的这个很棒的脚本之前。 .我需要确定。 ;) 谢谢!!
  • @GarrettKrosschell:我已经编辑了我的答案!只需使用json.dump(dictdump, o, indent=4)
  • @GarrettKrosschell 这应该没有任何效果,因为它是 json 格式,但如果你想保持订单,我可以编辑我的答案
  • 太棒了,艾伦。非常感谢你.. 我会接受你的回答,因为我知道它正在工作.. 我现在有一些编辑/脚本要学习.. :) ty。干杯。
猜你喜欢
  • 2015-03-02
  • 1970-01-01
  • 1970-01-01
  • 2018-09-21
  • 1970-01-01
  • 1970-01-01
  • 2016-06-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多