【问题标题】:Find rows in a file which doesnt contain a string and add text at start of it在不包含字符串的文件中查找行并在其开头添加文本
【发布时间】:2021-04-07 17:59:16
【问题描述】:

我的文件中有以下格式的数据。

name,path:A:B
loc:D
name,for:B:C

我需要在文件中所有那些没有 , 的行的开头添加“,”(空格 + 逗号)以获得如下所示的输出。

name,path:A:B
 ,loc:D
name,for:B:C

grep "[^,]" file .. 这给了我不包含的行列表,但我无法在开始时添加。

【问题讨论】:

  • 你有没有尝试过?
  • grep "[^,]" file .. 这给了我不包含的行列表,但我无法在开始时添加。

标签: shell awk sed grep


【解决方案1】:

grep 不是这里的工具,我会使用 awk 或 sed。使用 awk:

$ awk '
BEGIN {
    FS=OFS=","  # set delimiters to ,
}
NF==1 {         # if there is only one field (consider NF<=1 for ampty records)
    $1=OFS $1   # add a delimiter in front of it
}
1' file         # output

输出:

name,path:A:B
,loc:D
name,for:B:C

【讨论】:

    【解决方案2】:

    使用 sed:

    sed -r '/\,/!s/(^.*$)/ ,\1/' file
    

    使用 ! 搜索不带逗号的行然后将整行替换为空格、逗号和现有行。

    【讨论】:

    • 可以简化为sed '/\,/!s/^/ ,/'(另外,-E 是现在更常用的实现,而不是-r
    • 或者只是sed '/,/!s/^/ ,/' file;不需要 ERE 或捕获组。
    【解决方案3】:

    您能否尝试在 GNU awk 中使用所示示例进行跟踪、编写和测试。

    awk '!/,/{print OFS","$0;next} 1' Input_file
    

    说明:为上述添加详细说明。

    awk '               ##Starting awk program from here.
    !/,/{               ##Checking condition if line is NOT having comma then do following.
      print OFS","$0    ##Printing OFS comma and current line here.
      next              ##next will skip all further statements from here.
    }
    1                   ##1 will print current line here.
    ' Input_file        ##Mentioning Input_file name here.
    

    【讨论】:

      【解决方案4】:

      这里有很多使用sedawk 的好答案,这样做非常好(甚至比下面的更好)。但只是为了好玩,这里有一个仅限bash 的解决方案(无需调用grep 等外部程序或其他程序),它也可以解决问题:

      $ while read -r; do p=""; [[ ! $REPLY =~ [^,]*\,[^,]* ]] && p=" ,"; echo "${p}${REPLY}"; done < test.txt 
      name,path:A:B
       ,loc:D
      name,for:B:C
      

      解释版本:

      #!/bin/bash
      
      while read -r; do # looping over a file the right way in bash
        p="" # initializing a prefix
        [[ ! $REPLY =~ [^,]*\,[^,]* ]] && p=" ," # if the line doesn't contain a comma, the prefix will contain a comma
        echo "${p}${REPLY}"; # print the line with the prefix which can be blank or not
      done < test.txt 
      

      【讨论】:

        【解决方案5】:

        我会使用 GNU AWK 来完成这个任务,让 file.txt 内容成为

        name,path:A:B
        loc:D
        name,for:B:C
        

        然后

        awk '{print /,/?$0:" ,"$0}' file.txt
        

        输出

        name,path:A:B
         ,loc:D
        name,for:B:C
        

        说明:对于每一行,如果有,,则只有print 那行($0)否则print 连接空格逗号(" ,")和那行($0)。

        (在 gawk 4.2.1 中测试)

        【讨论】:

          【解决方案6】:

          要编辑文件本身,请使用ed:

          printf "%s\n" "v/,/s/^/ ,/" w | ed -s file
          

          在没有逗号的每一行的开头插入一个空格和逗号,然后保存修改后的文件。

          【讨论】:

            【解决方案7】:

            您可以使用index 来检查逗号。

            awk '{print index($0, ",") ? $0 : " ," $0}' file
            

            输出

            name,path:A:B
             ,loc:D
            name,for:B:C
            

            【讨论】:

              猜你喜欢
              • 2023-03-07
              • 1970-01-01
              • 2016-10-03
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-07-05
              • 2014-05-02
              相关资源
              最近更新 更多