linux - Repeating regex replace with SED -
i have next lines (in reality there ~1m of these lines):
foo|||bar qux||boo|fzx
note every line contain 4 fields, number of characters can more 3.
what want replace every||
|nil|
resulting:
foo|nil|nil|bar qux|nil|boo|fzx
what's way sed?
i tried fail:
sed 's/||/|nil/g'
you need repeat substitution until doesn't change:
sed ':a; s/||/|nil|/g; ta'
however not handle empty fields @ origin or end, need 2 more patterns:
sed 's/^|/nil|/; s/|$/|nil/; :a; s/||/|nil|/g; ta'
testing
input:
cat << eof > infile foo|||bar qux||boo|fzx ||| eof
run it:
<infile sed 's/^|/nil|/; s/|$/|nil/; :a; s/||/|nil|/g; ta'
output:
foo|nil|nil|bar qux|nil|boo|fzx nil|nil|nil|nil
an awk way awk '{ for(i=1;i<=nf;i++) if(length($i)==0) $i="nil" } 1' fs='|' ofs='|'
regex linux sed
No comments:
Post a Comment