Search strings and highlight them using Perl or bash/awk/sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search strings and highlight them using Perl or bash/awk/sed
# 8  
Old 08-11-2013
s#%#%&E#&%!!!

There was space after code in b.txt
Since I just copied it from post #1, I did not check.

Code:
cat -ev b.txt
GACAAGT $
AAAAAG $
TAATG$
CAAGC$
ACG $
GCTTG$

Now both awk and perl works fine.
Thanks.

Code:
awk 'NR==FNR {a[$0]=$0;next} {for (i in a) {gsub(a[i],"\033[1;31m&\033[0m",$0)}}1' b.txt a.txt
Seq1 -------------------------------TTAAAAAGTTTGAGTTCTAAA---------------- 21
Seq2 -----CTTGGCTCTTTCGTAAGTTTTTCATTAAGGAACTTGAATACACGGTTT----AC- 50
Seq3 TTAAACTTTTTTCAACCCTAATG-----CGGTTTGAACCATTAACC-----------TAAC 48
Seq4 --------GAAAGGAGCGGAGTG-GTCACGTGACAAGTTCTCAGACGCACGTGC--TTGT 49

This User Gave Thanks to Jotne For This Post:
# 9  
Old 08-12-2013
Thanks bartus and Jotne. Smilie
Can you please explain the code.

Thanks.
# 10  
Old 08-12-2013
Code:
awk '	NR==FNR {a[$0]=$0;next}
 	{for (i in a) 
		{gsub(a[i],"\033[1;31m&\033[0m",$0)}
	}1
	' b.txt a.txt

NR==FNR {a[$0]=$0;next}
NR==FNR This is a technique used to do something on the first file, when more file are listed, in this case b.txt
a[$0]=$0 Store every record of b.txt in an array named a eks a[GACAAGT]=GACAAGT
for (i in a) for every element in array a (b.txt), test it against a.txt
gsub(a[i],"\033[1;31m&\033[0m",$0) Test every element in array a, against the line $0 from a.txt, if found replace the found text with itself & plus ansi color code.
Eks if found AGC, replace it with \033[1;31mAGC\033[0m = AGC
Then the 1 at the final will print all lines from a.txt with modified colors for every find.



Edit: the $0 at the final is not needed, since this is the default line to test
Also changing that array name to b, to reflect its store content of b.txt to make it more clear.
Code:
awk 'NR==FNR {b[$0]=$0;next} {for (i in b) {gsub(b[i],"\033[1;31m&\033[0m")}}1' b.txt a.txt



Edit2: no need to have b.txt stored as both value and index of array b
Code:
awk 'NR==FNR {b[$0]++;next} {for (i in b) {gsub(i,"\033[1;31m&\033[0m")}}1' b.txt a.txt


Last edited by Jotne; 08-12-2013 at 03:34 AM..
This User Gave Thanks to Jotne For This Post:
# 11  
Old 08-12-2013
Thanks a lot. Smilie
I will try them.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to search and lossless replace strings using sed?

Hello, I would like to replace all occurencies of long data types by others (coresponding int) using 'sed' in the extensive source code of a software package written for 32 bit CPUs. I must use regular expressions to avoid wrong replacements like s/unsigned]+long/ulong/gLeaving out... (2 Replies)
Discussion started by: Mick P. F.
2 Replies

2. Shell Programming and Scripting

Rsync script to rewrite suffix - BASH, awk, sed, perl?

trying to write up a script to put the suffix back. heres what I have but can't get it to do anything :( would like it to be name.date.suffix rsync -zrlpoDtub --suffix=".`date +%Y%m%d%k%M%S`.~" --bwlimit=1024 /mymounts/test1/ /mymounts/test2/ while IFS=. read -r -u 9 -d '' name... (1 Reply)
Discussion started by: jmituzas
1 Replies

3. Shell Programming and Scripting

Display records between two search strings using sed

I have input file like AAA AAA CCC CCC CCC EEE EEE EEE EEE FFF FFF GGG GGG i was trying to retrieve data between two strings using sed. sed -n /CCC/,/FFF/p input_file Am getting output like CCC CCC CCC (1 Reply)
Discussion started by: NareshN
1 Replies

4. Shell Programming and Scripting

Advance search using sed/awk/perl

Hi, I have a file with more than 50,000 lines of records and each record is 50 bytes in length. I need to search every record in this file between positions 11-19 (9 bytes) and 32-40 (9 bytes) and in case any of the above 2 fields is alpha-numeric, i need to replace the whole 9 bytes of that... (7 Replies)
Discussion started by: kikionline
7 Replies

5. Shell Programming and Scripting

Using Bash/Sed to delete between identical strings

Hi. I'm hoping that someone can help me with a bash script to delete a block of lines from a file. What I want to do is delete every line between two stings that are the same, including the line the first string is on but not the second. (Marked lines to match with !) For example if I... (2 Replies)
Discussion started by: Zykr
2 Replies

6. Shell Programming and Scripting

Using Awk to Search Two Strings on One Line

If i wanted to search for two strings that are on lines in the log, how do I do it? The following code searches for just one string that is one one line. awk '/^/ {split($2,s,",");a=$1 FS s} /failure agaf@fafa/ {b=a} END{print b}' urfile What if I wanted to search for "failure agaf@fafa"... (3 Replies)
Discussion started by: SkySmart
3 Replies

7. Shell Programming and Scripting

Help with sed search&replace between two strings

Hi, i read couple of threads here on forum, and googled about what bugs me, yet i still can't find solution. Problem is below. I need to change this string (with sed if it is possible): This is message text that is being quoted to look like this: This is message text that is being quotedI... (2 Replies)
Discussion started by: angrybb
2 Replies

8. UNIX for Advanced & Expert Users

bash/grep/awk/sed: How to extract every appearance of text between two specific strings

I have a text wich looks like this: clid=2 cid=6 client_database_id=35 client_nickname=Peter client_type=0|clid=3 cid=22 client_database_id=57 client_nickname=Paul client_type=0|clid=5 cid=22 client_database_id=7 client_nickname=Mary client_type=0|clid=6 cid=22 client_database_id=6... (3 Replies)
Discussion started by: Pioneer1976
3 Replies

9. Shell Programming and Scripting

Awk search for a element in the list of strings

Hi, how do I match a particular element in a list and replace it with blank? awk 'sub///' $FILE list="AL, AK, AZ, AR, CA, CO, CT, DE, FL, GA, HI, ID, IL, IN, IA, KS, KY, LA, ME, MD, MA, MI, MN, MS, MO, MT, NE, NV, NH, NJ, NM, NY, NC, ND, OH, OK, OR, PA, RI, SC, SD, TN, TX, UT, VT, VA, WA,... (2 Replies)
Discussion started by: grossgermany
2 Replies

10. Shell Programming and Scripting

awk search for Quoted strings (')

Hi All, I have files: 1. abc.sql 'This is a sample file for testing' This does not have quotations this also does not have quotations. and this 'has quotations'. here I need to list the hard coded strings 'This is a sample file for testing' and 'has quotations'. So i have... (13 Replies)
Discussion started by: kprattip
13 Replies
Login or Register to Ask a Question