Home > OS >  Searching for a string between two characters
Searching for a string between two characters

Time:12-06

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

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

CodePudding user response:

With GNU awk. Separate the row into multiple columns using the : and - separators. In each row containing :, subtract the contents of column 2 from the contents of column 3 and print result.

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

Output:

4899

CodePudding user response:

Using awk

$ awk -F: '/:/ {split($2,a,"-"); print a[2] - a[1]}' input_file
4899
  • Related