我正在努力使用shell脚本将数据从具有下述相同格式的文件中提取到新的输出文件(CSV)中。
[+++] Added Names: [+++]
-> Add- abc.txt (732):
-> Add- bcd.txt (490):
[+++] Enabled Names: [+++]
-> Mod- cde.txt (105):
-> Mod- efg.txt (105):
[+++] Deleted Names: [+++]
-> Del- fgh.txt (4):
-> Del- xyz.txt (45):
[+++] Added non-names lines: [+++]
-> Added to test.txt (41):
-> Added to bgh.txt (41):
# This distribution may contain names under different licenses.
并且预期输出应该看起来像
Add,abc.txt,732
Add,bcd.txt,490
Mod,cde.txt,105
Mod,efg.txt,105
Del,fgh.txt,4
Del,xyz.txt,45
此处输出重命名与最后一样。唯一不需要的是 - >添加到test.txt(41):和 - >添加到bgh.txt(41): .which是 [+++]的一部分添加了非名称行:[+++]
答案 0 :(得分:3)
您可以将此awk
命令与自定义输入字段分隔符一起使用:
awk -v OFS=, -F '[->:()[:blank:]]+' 'index($0, "[+++]") {
p=($0 ~ / (Added|Deleted|Enabled) Names:/); next} p && /-> /{print $2, $3, $4}' file
Add,abc.txt,732
Add,bcd.txt,490
Mod,cde.txt,105
Mod,efg.txt,105
Del,fgh.txt,4
Del,xyz.txt,45
答案 1 :(得分:1)
使用gsub
函数的另一种方法: -
awk '/->/{gsub(/[->():]/,X);$1=$1;print}' OFS=, file
Add,abc.txt,732
Add,bcd.txt,490
Mod,cde.txt,105
Mod,efg.txt,105
Del,fgh.txt,4
Del,xyz.txt,45
答案 2 :(得分:1)
另一个GNU awk
解决方案:
awk -v FPAT='[[:alnum:].]+' '/->/{ print $1,$2,$3 }' OFS=',' file
输出:
Add,abc.txt,732
Add,bcd.txt,490
Mod,cde.txt,105
Mod,efg.txt,105
Del,fgh.txt,4
Del,xyz.txt,45