shell - Repeatly replace a delimiter at a given count (4), with another character -
given line:
12,34,56,47,56,34,56,78,90,12,12,34,45
if count of commas(,) greater four, replace 4th comma(,) ||
.
if count lesser or equal 4 no need replace comma(,).
i able find count following awk
:
awk -f\, '{print nf-1}' text.txt
then used if condition check if result greater 4. unable replace 4th comma ||
find count of delimiter in line , replace particular position character.
update:
i want replace comma ||
symbol after every 4th occurrence of comma. sorry confusion.
expected output:
12,34,56,47||56,34,56,78||90,12,12,34||45
using awk can do:
s='12,34,56,47,56,34,56,78,90,12,12,34,45' awk -f, '{for (i=1; i<nf; i++) printf "%s%s", $i, (i%4?fs:"||"); print $i}' <<< "$s" 12,34,56,47||56,34,56,78||90,12,12,34||45
Comments
Post a Comment