我有一行文字,如:
value,value,,,value,value
我希望用不同的值替换两个空值。就像是:
value,value,hello,hello,value,value
我目前正在尝试类似的东西:
sed -e 's/^,/hello,/' -e 's/,$/,hello/' -e 's/,,/,hello,/g'
这会在开头、结尾和中间捕获空白值。但是,它不会连续捕获两个连续的空白值。如何更新正则表达式模式,以便在中间出现无限数量的连续空白值?
最佳答案
这个 perl one-liner 怎么样:
echo ',value,,,value,' | perl -pe 's/^(?=,)|(?<=,)(?=,|$)/hello/g'
# hello,value,hello,hello,value,hello
关于regex - 连续替换 CSV 文件中的空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6431573/