原始档案:
{
A:2,
B:2,
C:2,
}
必需:
{
A:2,
B:2,
C:2
}
有没有办法在一行中使用awk或sed来做到这一点?
答案 0 :(得分:2)
使用GNU awk:
$ cat file
{
A:2,
B:2,
C:2,
}
$ gawk -v RS='\0' -v ORS= '{print gensub(/,([^,]+)$/,"\\1","")}' file
{
A:2,
B:2,
C:2
}
如果您需要对包含多个块的文件执行此操作,请更改以下脚本:
gawk -v RS='\0' -v ORS= '{print gensub(/,([^,]+)$/,"\\1","")}' file
为:
gawk -v RS='\0' -v ORS= '{print gensub(/,([^,]+})/,"\\1","g")}' file
e.g:
$ cat file
{
A:2,
B:2,
C:2,
}
{
D:2,
E:2,
F:2,
}
$
$ gawk -v RS='\0' -v ORS= '{print gensub(/,([^,]+})/,"\\1","g")}' file
{
A:2,
B:2,
C:2
}
{
D:2,
E:2,
F:2
}