I have this line:
"a | a | a | a | a | a | a | a"
and I want to replace each "|" to the incremental value as follows:
"a0a1a2a3a4a5a6a"
I know that I can use gsub to replace strings:
> echo "a | a | a | a | a | a | a | a" | awk '{gsub(/\ \|\ /, ++i)}1'
a1a1a1a1a1a1a1a
But it seems that gsub only increases after each new line, so my solution will now first put a new line after each "|", and then use gsub again and delete the newline characters:
> echo "a | a | a | a | a | a | a | a" | awk '{gsub(/\ \|\ /, " | \n")}1' | awk '{gsub(/\ \|\ /, ++i)}1' | tr -d '\n'
a1a2a3a4a5a6a7a
It's honestly just disgusting ...
Is there a better way to do this?
source
share