连续替换 CSV 文件中的空值 [英] Replacing consecutively empty values in a CSV file
本文介绍了连续替换 CSV 文件中的空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一行文字,如:
value,value,,,value,value
我希望用不同的值替换两个空值.类似的东西:
I want the two empty values to be replaced with a different value. Something like:
value,value,hello,hello,value,value
我目前正在尝试以下方法:
I'm currently trying something like:
sed -e 's/^,/hello,/' -e 's/,$/,hello/' -e 's/,,/,hello,/g'
这会在开头、结尾和中间捕获空白值.但是,它不会连续捕获两个连续的空白值.如何更新正则表达式模式,以便在中间出现无限数量的连续空白值?
That catches blank values at the beginning, end and the middle. BUT, it doesn't catch two consecutive blank values in a row. How can I update the regular expression patterns so that an indefinite number of consecutive blank values can appear in the middle?
推荐答案
这个 perl one-liner 怎么样:
What about this perl one-liner:
echo ',value,,,value,' | perl -pe 's/^(?=,)|(?<=,)(?=,|$)/hello/g'
# hello,value,hello,hello,value,hello
这篇关于连续替换 CSV 文件中的空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文