我在檔案中具有以下格式的資料。
name,path:A:B
loc:D
name,for:B:C
我需要在檔案中所有没有的行的開頭添加","(空格+逗號),以得到如下所示的輸出。
name,path:A:B
,loc:D
name,for:B:C
grep "[^,]" file
..這给了我不包含的行列表,但是我無法在開始時添加。
最新回復
- 5月前1 #
- 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
相似問題
- bash:使用sed批量重命名檔案bashshellsedfilerename2021-01-09 17:28
- regex:仅使用sed或awk从html頁面提取URL的最簡單方法htmlregexbashsedawk2021-01-09 16:56
- 删除帶有awk或sed的列sedawk2021-01-08 02:25
- regex:Grep第一行包含日期regexawksed2021-01-07 00:54
- bash:查詢唯一名稱的频率bashshellscripttextprocessingawksed2021-01-06 13:54
grep
不是這裏的工具,我会使用awk或sed.使用awk:輸出: