首頁>Program>source

我在檔案中具有以下格式的資料。

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

我需要在檔案中所有没有的行的開頭添加","(空格+逗號),以得到如下所示的輸出。

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

grep "[^,]" file ..這给了我不包含的行列表,但是我無法在開始時添加。

最新回復
  • 5月前
    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
    

  • 5月前
    2 #

    能否請您尝試按照GNU awk中顯示的示例进行以下操作,編寫和測試

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

    Explanation: 為此添加了详细說明。

    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.
    

  • 5月前
    3 #

    使用sed:

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

    使用!搜尋没有逗號的行! 然後用整行替換空格,逗號和現有行。

  • 5月前
    4 #

    我將使用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中进行了測試)

  • 5月前
    5 #

    您可以使用 index 檢查逗號。

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

    輸出

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

  • 使用dplyr和lubridate按月和年組合計數和分組
  • 在發佈合並請求之前執行gitlab管道