使用Sed,awk或tr,如何在特定字符后添加换行符?

时间:2017-03-15 12:05:27

标签: awk sed tr

我有这个文件:

lol.txt:hungrylol2.txt:hungry
lol3.txt:hungry
lol4.txt:hungry
lol5.txt:hungrylol6.txt:hungry

我想转入这一个:

lol.txt:hungry
lol2.txt:hungry
lol3.txt:hungry
lol4.txt:hungry
lol5.txt:hungry
lol6.txt:hungry

我已经用sed,tr和awk进行了调查,但没有找到方便的方法。

谢谢

3 个答案:

答案 0 :(得分:0)

尝试:

library(shiny)
library(DT)

ui <- fluidPage(
  dataTableOutput("table")
)

server <- function(input, output,session) {
  v<-reactive({
    temp<-data.frame(TBL.name=paste("Data ",1:10))
    temp<-cbind(
      temp,
      #Dynamically create the download and action links
      Action=sapply(seq(nrow(temp)),function(i){as.character(actionLink(paste0("Action_",i),label = paste0("Action ",i),onclick=paste0('Shiny.onInputChange(\"action_click\", (this.id))'),class="btn"))}),
      Download=sapply(seq(nrow(temp)),function(i){as.character(downloadLink(paste0("downloadData_",i),label = paste0("Download Data ",i)))})
    )
    DT::datatable(temp,rownames = FALSE,options = list(dom = 't'),selection = "none",class='cell-border strip hover',escape = FALSE)
  })

  output$table<-renderDataTable({
    v()
  })

  #Events on view that will bring display a modal and delete that deletes the data
  observeEvent(input$action_click,{print(input$action_click)})

  observe({
    lapply(seq(nrow(v())),function(i){
      output[[paste0("downloadData_",i)]] <- downloadHandler(

        filename = function() {
          paste("data-",i, Sys.Date(), ".zip", sep="")
        },
        content = function(file) {
          # link to file that will be downloaded
        }
        # contentType = "application/zip"
      )

    })
  })    
}

# Run the application 
shinyApp(ui = ui, server = server)

我全局用饥饿的新行和lol替换字符串hungrylol,然后打印Input_file行。

答案 1 :(得分:0)

在&#34;饥饿&#34;之后添加一个新行。如果没有一个:

$ sed -r 's/(hungry)([^\n])/\1\n\2/g' file
lol.txt:hungry
lol2.txt:hungry
lol3.txt:hungry
lol4.txt:hungry
lol5.txt:hungry
lol6.txt:hungry

这匹配hungry后跟不是新行的字符。发生这种情况时,它会将hungry +新行+替换为已捕获的字符。

答案 2 :(得分:0)

使用BRE使用sed:

sed 's/\(hungry\)\(.\)/\1\n\2/' file

hungry后跟一个字符时,请输出一个新行和捕获的字符。