Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Searching for a string between two characters

I need to find two numbers from lines which look like this

>Chr14:453901-458800

I have a large quantity of those lines mixed with lines that doesn’t contain ":" so we can search for colon to find the line with numbers. Every line have different numbers.

I need to find both numbers after ":" which are separated by "-" then substract the first number from the second one and print result on the screen for each line

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’d like this to be done using awk

I managed to do something like this:

awk -e '$1 ~ /\:/ {print $0}' file.txt

but it’s nowhere near the end result

For this example i showed above my result would be:

4899

Because it is the result of 458800 - 453901 = 4899

I can’t figure it out on my own and would appreciate some help

>Solution :

With GNU awk. Use : and - as field separators:

awk -F '[:-]' '/:/{print $3-$2}' file

Output:

4899
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading