Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

bash return position of matching string

# 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
Comment

PREVIOUS NEXT
Code Example
Shell :: linux saber la distribución terminal 
Shell :: how to overwrite symlink linux 
Shell :: get disk utilization linux 
Shell :: install php on wsl2 
Shell :: how to zip with tar command 
Shell :: command prompt cd back 
Shell :: untar tar.gz 
Shell :: firebase deploy only hosting command 
Shell :: asdf current 
Shell :: install jq on windows 
Shell :: telegram on archlinux 
Shell :: download wine for ubuntu 
Shell :: find a file linux 
Shell :: update git from terminal 
Shell :: open local files wsl 
Shell :: docker run restart on boot 
Shell :: powershell get process name by port 
Shell :: flutter reinstall pod 
Shell :: fix brew 
Shell :: start apache server kali 
Shell :: how to open boot config raspberry pi command propt 
Shell :: python pip upgrade 
Shell :: git pull in all repositories 
Shell :: searching for a directory powershell 
Shell :: set up redux in react 
Shell :: g++ use c++20 
Shell :: get public ipv6 linux 
Shell :: git commit ignore eslint 
Shell :: git second commit 
Shell :: bash concatenate gzipped files 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =