gpt4 book ai didi

shell - 在不包含字符串的文件中查找行并在其开头添加文本

转载 作者:行者123 更新时间:2023-12-04 16:16:50 25 4
gpt4 key购买 nike

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

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

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

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

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

最佳答案

您能否尝试使用 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.

关于shell - 在不包含字符串的文件中查找行并在其开头添加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65520382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com