在另一个文本文件1中补充或删除文本文件2的元素

时间:2017-04-21 12:54:47

标签: python unix awk sed grep

有关获取两个文件的差异的最佳方法的任何帮助,或者换句话说,文件的赞美?在Unix或Shell脚本或某些直接python实用程序中的东西?

让我们说:文件1在下面。

  • A
  • A
  • A
  • d
  • 电子
  • 电子
  • F
  • 15
  • A

文件2的文字如下:

  • A
  • B
  • d
  • 电子
  • A

众所周知,文件2是文件1的子集,输出应该是:从文件1中删除文件2中第一次出现的元素。所以输出如下所示:

  • A
  • A
  • 电子
  • ˚F
  • 15

换句话说,输出只不过是文件1中文件2的补充。(订购并不重要)

3 个答案:

答案 0 :(得分:1)

  library(shiny)
  library(shinydashboard)
  library(shinysky)
  library(shinyjs)

  shinyApp(
    ui=shinyUI(dashboardPage(skin = "blue",
                             dashboardHeader(title = "Dashboard"),
                             dashboardSidebar(
                               sliderInput("obs", "Number of observations:", 
                                           min = 10000, max = 90000, value = 50000, step = 10000)
                             ),
                             dashboardBody(
                               useShinyjs(),

                               tags$body(inlineCSS(list(".shinysky-busy-indicator" = "position: absolute !important; z-index:800; "))),

                               tabBox(
                                 tabPanel("Tab 1", 
                                          busyIndicator(wait = 1000),
                                          "This is tab number 1",
                                          tableOutput("tabData")),
                                 tabPanel("Tab 2", 
                                          busyIndicator(wait = 1000),
                                          "This is tab number 2", 
                                          plotOutput("tabPlot"))
                               )
                             )
    ))
    ,
    server=shinyServer(function(input, output) {

      calculate_hist <- reactive({
        cat("Calculating for ",input$obs,"\n")
        if (is.null(input$obs)) {
          dist <- NULL
        }else{
          dist<-numeric()
          for (i in 1:input$obs) dist <- c(dist, rnorm(1))
        }
        cat("Ended\n")
        dist
      })
      output$tabPlot<-renderPlot({
        chist<-calculate_hist()
        if (is.null(chist)){
          return()
        }else{
          hist(chist, breaks = 100)
        } 
      })
      output$tabData<-renderTable({
        chist<-calculate_hist()
        if (is.null(chist)){
          return()
        }else{
          sumdata<-data.frame(n=length(chist),mean=mean(chist),var=var(chist))
          sumdata
        }
      })
    })
  )

会给你:

awk 'NR==FNR{a[NR]=$0;n=NR;next}
    {for(i=1;i<=n;i++)if($0==a[i]){delete a[i];next}print}' file2 file1

代码很简单并且告诉他们做了什么。

答案 1 :(得分:0)

您可以使用python

file_1_data = open('file_1.txt').read().split('\n')   
file_2_data = open('file_2.txt').read().split('\n')

for data in file_2_data:
    if data in file_1_data:
        file_1_data.remove(data)

open('file_1.txt','w').write('\n'.join(file_1_data))

答案 2 :(得分:0)

comm最适合此任务,但需要排序输入

$ comm -23 <(sort file1) <(sort file2)
15
A
A
B
E
F

来自man comm

   comm - compare two sorted files line by line

   -2     suppress column 2 (lines unique to FILE2)

   -3     suppress column 3 (lines that appear in both files)


如果输出需要按预期输出

所示进行排序
$ comm -23 <(sort file1) <(sort file2) | sort -n
A
A
B
E
F
15
相关问题