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

标签 shell awk sed grep

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

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/

相关文章:

java - 为什么 nohup 在这种情况下不起作用?

linux - 无法使 bash 脚本从 cloud-init 运行

shell - 在文件中搜索多个模式并在模式匹配时删除行

shell - 使用 If-Then-Else 将文件拆分为 3 个文件

bash - 从 40900000 拆分为 409-00-000

linux - 将多行变成一个逗号分隔的行

sed - 使用 sed 改变 Makefile 变量

awk - 如何在linux中awk仅选择列并输出

perl - 在 perl 中运行 awk 命令

linux - 使用命令行工具获取apache服务器版本