Visit Our UNIX and Linux User Community

Bash script - coloring reg. expressions in text

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script - coloring reg. expressions in text
# 1  
Old 10-30-2012
Bash script - coloring reg. expressions in text

Hi all,
is there anyone good at bash who will help me?

I need to use syntax ${string/pattern/replacement}
The problematic part where I am stuck is:

text="A cat is on a mat."

 == > ERROR "wrong substitution"
 == > ERROR "wrong substitution"
newtext=${"cat is here"/"cat"/"dog"}
 == > newtext is EMPTY STRING - Why not "dog is here" ?

Any ideas what i am doing wrong? Or would you approach it differently?
Thanks for your help.

As for the use, I am to create a bash file that
1) Gets pairs color-regex, such as: r 'bar[a-z]*\>' g 'text' b 'reg'
2) Reads user's input and colors each input line properly.
# 2  
Old 10-30-2012

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 10-30-2012
Works on Solaris via PuTTY emulating a vt100 terminal:

$ cat x

# Define terminal escape sequences.
GREEN="\033[1;32m"  # bold, green

text="A cat is on a cat mat."  # Added a second "cat"

# 1 slash matches the first occurance, 2 matches all occurances.
echo -e "$newtext"

exit 0
$ ./x
A cat is on a cat mat.

Actually, this is closer to your original request:


text="A cat is on a cat mat."

echo -e "$newtext"

exit 0

Last edited by gary_w; 10-30-2012 at 04:07 PM..
These 2 Users Gave Thanks to gary_w For This Post:
# 4  
Old 10-30-2012

Thanks a lot! Smilie The documentation pretty much sucks when it says ${string/pattern/replacement} , but only pattern and replacement can have dollar signs even though all three are variables Smilie
# 5  
Old 10-30-2012
Actually the leading dollar sign in ${string/pattern/replacement} tells the shell to perform the search/replace on $string (in this example $text).
In other words, $string is the variable the search/replace will operate on. That's why you surround it in curly braces.

Previous Thread | Next Thread
Test Your Knowledge in Computers #851
Difficulty: Medium
Angular is a TypeScript-based open-source web application framework.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Going mad on an egrep command (Reg Expressions)

Dear community, I am trying for several hours now to create an egrep command to grep the number of lines containing a specific text from a text-file but seem to have an error somewhere. The Textfile contains several thousand lines and has the expression "Lastname" in several lines.... (3 Replies)
Discussion started by: Donzo
3 Replies

2. Shell Programming and Scripting

BASH - Regular Expressions :Looking for one word on multiple lines.

Im looking for a bash solution that will use Regular Expressions (not perl, sed or awk) to check the example data below and then give me a status. which would be just simply Match or Mismatch. SYS PS1 is present. Fan status: Normal Input Voltage status: Normal ... (5 Replies)
Discussion started by: popeye
5 Replies

3. Shell Programming and Scripting

Problems with reg.-expressions in a awk-search-pattern

Hi, i have some problems with regular expressions in a awk search pattern. what i want to do: i want to calculate the mean-value in the time from 00:00 to 06:00 how my data looks like: .... 04/01/13-01:40 670992 54802 80711 116460 156177 04/01/13-01:50 703725 60150 85498 ... (3 Replies)
Discussion started by: IMPe
3 Replies

4. Shell Programming and Scripting

Bash script reg-exp , replace , open and write

Hi All I am a new in scripting language and I would like help for you guys I would like to create a file named constant.h and search into all files *.m in specific directory for a reg-exp @"LBL_]+" exp: @"LBL_75847" , and write those matchs to constant.h if there are not written (no... (15 Replies)
Discussion started by: molwiko
15 Replies

5. Shell Programming and Scripting

How to remove text between two expressions

I need to remove a section of text on a line that is between two patterns. This is the line: Begin : Transaction Id 00004d9e769065ab 04-Oct-2011 04:01:48.69 Username smith I need the line to read: Begin : 04-Oct-2011 04:01:48.69 Username smith... (8 Replies)
Discussion started by: deneuve01
8 Replies

6. Shell Programming and Scripting

Reg expressions

Hi, I would like to grep for a string within a tag, can someone provide some assistance in how to do it? So I would like to use the grep command to find a string like: <tag>sometext<tag> because the sometext can be any number of characters or an type of number or lettering, what expression... (1 Reply)
Discussion started by: cyberfrog
1 Replies

7. Shell Programming and Scripting

Complex coloring in script

My script prints lines in which the entire line may be colored, and portions may also be colored. e.g. Consider this to be one line: $red some text in red $yellow abcd $end_yellow red text 1234 $blue some text $end_blue more red text $end_red So using sed, I may based on condition 1,... (5 Replies)
Discussion started by: sentinel
5 Replies

8. Shell Programming and Scripting

How to extract text from string using regular expressions

Hi, I'm trying to use sed to extract some text and assign it to a variable. Can anyone provide me with some help? it would be much appreciated! I"m looking to extract for example: filename=/output/R34/2005_13_R34_C1042S_T83_CRFTXT_20081015.txt I'm trying to extract the 1042... (9 Replies)
Discussion started by: jtung
9 Replies

9. UNIX for Dummies Questions & Answers

Coloring personal text in vim

Hi, I want to color some personal text, such as my own name in vim editor. Can anyone tell me how this is done. Thanks, Sathya (1 Reply)
Discussion started by: skkrish2
1 Replies

10. UNIX for Advanced & Expert Users

SCO vs Linux Reg Expressions Problem

Hi there, I'm investigating migrating a system currently running Scos osr5 to Linux (eg RH 7.2) but there are a lot of in house scripts, some of which are probably using "Sco specific" constructs etc. One I have come across is as follows, if ????? ] then ....etc.etc fi The regular... (2 Replies)
Discussion started by: pcs7088
2 Replies

Featured Tech Videos