LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Closed Thread
  Search this Thread
Old 03-02-2023, 02:59 AM   #1
Faki
Member
 
Registered: Oct 2021
Posts: 574

Rep: Reputation: Disabled
Printing a coloured multiline string in awk script


I want to print a multiline string in colour using lines with colour tags ("Grn:" for green; "Blu:" for blue, ...).

Here is an example multiline string

Code:
  mstr=" \n \
 Grn: \n \
   Some text in green colour \n \
   More text here \n \
 Blu: \n \
   Some Blue text \n \
   More blue text"
Here is a function that I have constructed. Where I split the multiline string into an array. What I need to de new is match any of the "tseq[knam]" indices in "knam" when they occur in the string.

It seems that GSUB cannot take the fourth argument.

Code:
function lumer(str) {

  kl=0
  kmd="tput sgr0"
  rst = ( (kmd | getline outp) > 0 ? outp : "<" "sgr0" ">" )
  close(kmd)
  ka = "Wht 15 Grn 34 Blu 39 Ylw 11 Red 196 Amb 214"
  kb = "Cyn 51 Mgn 201 Syp 39 Code 39"
  ks = sprintf("%s %s", ka, kb)
  n = split(ks, kaggr)  # split makes KAGGR start at index 1
  for ( i=1; i<n; i+=2 ) {
    knam = kaggr[i] ":"
    knum = kaggr[i+1]
    kmd=sprintf("%s%d\n", "tput bold; tput setaf ", knum)
    tseq[knam] = ( (kmd | getline kseq) > 0 ? kseq : "<" knam ">" )
    close(kmd)
   }

  ## Split with newline character, and store in array ASTR
  nlines = split(mstr, astr, "\n")
  for (i = 1; i <= nlines; i++) {

  ## Capture KNAM from TSEQ[KNAM] and match with string ASTR[I]
  for ( knam in tseq ) {
    ## Remove any blanks from string
    str = "" ; gsub(/[[:blank:]]+/, "", astr[i], str)
    if ( knam == str ) {
      print "Matched line: " astr[i]
     }
   }
  print i " " astr[i]
  }
 }

Last edited by Faki; 03-02-2023 at 03:57 AM.
 
  


Closed Thread

Tags
awk



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Printing multiline string with %s Faki Linux - Newbie 7 02-16-2023 12:51 AM
[SOLVED] Printing coloured sections Faki Linux - Newbie 4 02-13-2023 01:14 PM
Extarct tags with multiline values from XML file using sed/Awk gbms Linux - Newbie 3 03-27-2012 10:18 AM
[SOLVED] Using grep / awk to search for coloured text s7upify Linux - General 5 09-21-2010 11:00 AM
[SOLVED] Awk & multiline webhope Programming 9 05-24-2010 03:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration