0

Is it possible to process a multiline variable in a bash function? Suppose I have a function theone and I pass a multiline variable to it.

var="
This
is a 
multiline
variable"

theone "$var"

I want to colour specific lines from var according to some matching pattern without using awk or sed.

Vera
  • 1,223
  • Be precise/concise about what do you want to do with this multi-line sample – Gilles Quénot Feb 16 '23 at 18:47
  • Seems like you have lot of responses here or on stackoverflow about this topic, mostly based on awk. https://unix.stackexchange.com/questions/735367/line-colouring-fails-for-syp-and-code https://unix.stackexchange.com/questions/735244/handling-multiple-colour-declarations-for-colour-printing Whats wrong with those responses? – Gilles Quénot Feb 16 '23 at 18:56
  • Related: https://stackoverflow.com/questions/64034385/using-awk-to-color-the-output-in-bash/64046525#64046525 – Gilles Quénot Feb 16 '23 at 19:00
  • There is nothing wrong. But as the number and length of awk commands increasing, I started putting everything in separate awk files. One problem with awk files is the difficulty of integrating different implementations in a single awk file. Furthermore, multiple branching make awk files inefficient because the tests are done for every line being read. – Vera Feb 16 '23 at 19:28

1 Answers1

0

As a starter:

#!/bin/bash

theone() { printf -- '%s\n' "$var" | while IFS= read -r line; do tput setaf $((++c)) echo "$line" ((c==8)) && c=0 done tput sgr0 }

theone "$var"

enter image description here