Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SHELL

awk get index of matching substring

# Basic syntax:
grep search_string your_file | awk '{ print index($0, "search_string") }'
# Where:
#	- grep finds the lines in your_file that contain the search_string
#	- the awk index function returns the 1-based index of the search_string
#		within the entire string (which is given by $0 in awk)

# Example usage:
# Say you have a file with the following DNA sequences and want to know where
# the substring CTGCAGTAAG appears within them
CACTATAGGGCTGCAGTAAGCGCGTCGC
CTATAGGGCTGCAGTAAGCGCGTCGCGG
CAATAGGGCTGCAGTAAGCGCGTCGCTT
CACTATAGGGCTGCAGTAAGCGCGTCGC
CACTGGGCTGCAGTAAGCGCGTCGCCCC
CATAGGGCTGCAGTAAGCGCGTCGCGGG
CACTATAGGGCTGCAGTAAGCGCGTCGC
# running:
grep CTGCAGTAAG your_file | awk '{ print index($0, "CTGCAGTAAG") }'
# would return:
11
9
9
11
8
8
11

# Example usage 2:
# If you want to get the frequency of each index sorted from high to low,
# you could run a command like:
grep CTGCAGTAAG your_file | awk '{ print index($0, "CTGCAGTAAG") }' | sort | uniq -c | sort -nr
3 11
2 9
2 8
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #awk #index #matching #substring
ADD COMMENT
Topic
Name
6+1 =