tcl extract value from string - compare

I have the following context:
Radio power mode = ON
Current Band = GSM 900, Channel Number = 63
Current RSSI = -95 dBm
Band Selected = Auto
Number of nearby cells = 1
Cell 1
Primary Scrambling Code = 0x1C4
RSCP = -115 dBm, ECIO = -16 dBm
I need extract the value from the line "Current RSSI", and check based on the list below:
- > -60 dBM = Solid
- <= –60 to 74 dBm = Very strong signal
- <= –75 to 89 dBm = Strong signal
- <= –90 to 109 dBm = Fair signal
- <= –110 dBm = Unusable signal

You can use the regexp command to pull the number out of the string.
Something like this, perhaps:
regexp {Current RSSI = ([^ ]+)} $the_data match rssi
That will return 1 if the string was found, so you can use a conditional statement like this:
if {[regexp ...]} {
# the match was found
...
}
Given your data, rssi should contain the string "-95". You can then convert that to an integer and use it to compute the string.

You populate two variables:
- match which holds the total matching string, which is "Current RSSI = -95"
- rssi which contains just the substing enclosed in parenthesis in the regexp, which is -95.

Related

Regex to get certains number of string - Python

I need to get price of this strig "Prix\xa0de base : 26 900 euros – bonus" but there is a 0 in 'Prix\xa0de' and I don't know how to do it.
Thanks for your help!
You can use something like this:
subject = "Prix\xa0de base : 26 900 euros – bonus"
match = re.search(r"^.*:\s+([\d ]+)\s+", subject)
if match:
result = match.group(1)
else:
result = ""
result will be 26 900
If it always is followed by the word 'euros' then as simple as:
'(\d+ ?\d+) euros'
Capturing the number (or number with a space as separator) before 'euros'

Using regex to capture phone numbers with spaces inserted at differing points

I want to be able to extract a complete phone number from text, irrespective of how many spaces interrupt the number.
For example in the passage:
I think Emily was her name, and that her number was either 0421032614 or 0423 032 615 or 04321 98 564
I would like to extract:
0421032614
0423032615
0432198564
I can extract the first two using
(\d{4}[\s]?)(\d{3}[\s]?)+
But this is contingent on me knowing ahead of time how the ten numbers will be grouped (i.e. where the spaces will be). Is there any way to capture the ten numbers with a more flexible pattern?
You need to remove all white space then run a for loop and iterate through the groups:
public static void main (String [] args){
String reg = "(\\d{10})";
String word = " think Emily was her name, and that her number was either 0421032614 or 0423 032 615 or 04321 98 564";
word = word.replaceAll("\\s+",""); // replace all the whitespace with nothing
Pattern pat = Pattern.compile(reg);
Matcher mat = pat.matcher(word);
while (mat.find()) {
for (int i = 1; i <= mat.groupCount(); i++) {
System.out.println(mat.group(i));
}
}
}
output is
0421032614
0423032615
0432198564

How to remove numbers from text in Scala?

How to remove numbers form a text in Scala ?
for example i have this text:
canon 40 22mm lens lock strength plenty orientation 321 .
after removing :
canon lens lock strength plenty orientation .
Please, try filter or filterNot
val text = "canon 40 22mm lens lock strength plenty orientation 321 ."
val without_digits = text.filter(!_.isDigit)
or
val text = "canon 40 22mm lens lock strength plenty orientation 321 ."
val without_digits = text.filterNot(_.isDigit)
\\d+\\S*\\s+
Try this.Replace by empty string.See demo.
https://regex101.com/r/tS1hW2/1
Since it is apparent, you want to remove all words that contain a number, because in your example mm is also gone, because it is prefixed by a number.
val s = "That's 22m, which is gr8."
s.split(" ").filterNot(_.exists(_.isDigit)).mkString(" ")
res8: String = That's which is

Error in writing output file through AWK scripting

I have a AWK script to write specific values matching with specific pattern to a .csv file.
The code is as follows:
BEGIN{print "Query Start,Query End, Target Start, Target End,Score, E,P,GC"}
/^\>g/ { Query=$0 }
/Query =/{
split($0,a," ")
query_start=a[3]
query_end=a[5]
query_end=gsub(/,/,"",query_end)
target_start=a[8]
target_end=a[10]
}
/Score =/{
split($0,a," ")
score=a[3]
score=gsub(/,/,"",score)
e=a[6]
e=gsub(/,/,"",e)
p=a[9]
p=gsub(/,/,"",p)
gc=a[12]
printf("%s,%s,%s,%s,%s,%s,%s,%s\n",query_start, query_end,target_start,target_end,score,e,p,gc)
}
The input file is as follows:
>gi|ABCDEF|
Plus strand results:
Query = 100 - 231, Target = 100 - 172
Score = 20.92, E = 0.01984, P = 4.309e-08, GC = 51
But I received the output in a .csv file as provided below:
100 0 100 172 0 0 0 51
The program failed to copy the values of:
Query end
Score
E
P
(Note: all the failed values are present before comma (,))
Any help to obtain the right output will be great.
Best regards,
Amit
As #Jidder mentioned, you don't need to call split() and as #jaypal mentioned you're using gsub() incorrectly, but also you don't need to call gsub() at all if you just include , in your FS.
Try this:
BEGIN {
FS = "[[:space:],]+"
OFS = ","
print "Query Start","Query End","Target Start","Target End","Score","E","P","GC"
}
/^\>g/ { Query=$0 }
/Query =/ {
query_start=$4
query_end=$6
target_start=$9
target_end=$11
}
/Score =/ {
score=$4
e=$7
p=$10
gc=$13
print query_start,query_end,target_start,target_end,score,e,p,gc
}
That work? Note the field numbers are bumped out by 1 because when you don't use the default FS awk no longer skips leading white space so there's an empty field before the white space in your input.
Obviously, you are not using your Query variable so the line that populates it is redundant.

Add two different numbers in a single text field space separated in Access VBA

I am using Access and VBA to tidy up a database before a migration. One field is going from text to an INT. So I need to convert and possibly add some numbers which exist in a singular field.
Examples:
F/C 3 other 8 should become 11
Calender-7 should become 7
21 F/C and 1 other should become 22
29 (natural ways) should become 29
The second and fourth line are simple enough, just use the following regex in VBA
Dim rgx As New RegExp
Dim inputText As String
Dim outputText As String
rgx.Pattern = "[^0-9]*"
rgx.Global = True
inputText = "29 (natural ways)"
outputText = rgx.Replace(inputText, "")
The downside is if I use it on option 1 or 3:
F/C 3 other 8 will become 38
Calender-7 will become 7
21 F/C and 1 other will become 211
29 (natural ways) will become 29
This is simple enough in bash, I can just keep the spaces by adding one to [^0-9 ]* and then piping it into awk which will add every field using a space as a delimiter like so:
sed 's/[^0-9 ]*//g' | awk -F' ' 's=0; {for (i=1; i<=NF; i++) s=s+$i; print s}'
F/C 3 other 8 will become 11
21 F/C and 1 other will become 22
The problem is I cannot use bash, and there are far too many values to do it by hand. Is there any way to use VBA to accomplish this?
Instead of using the replace method, just capture and then add up all the numbers. For example:
Option Explicit
Function outputText(inputText)
Dim rgx As RegExp
Dim mc As MatchCollection, m As Match
Dim I As Integer
Set rgx = New RegExp
rgx.Pattern = "[0-9]+"
rgx.Global = True
Set mc = rgx.Execute(inputText)
For Each m In mc
I = I + CInt(m) 'may Need to be cast as an int in Access VBA; not required in Excel VBA
Next m
outputText = I
End Function
I'm not sure if there are any easier way for your question. Here I've wrote small function for you.
Requirement: add all numbers in a string, identify "consecutive" digits as one number.
pseudo:
Loop through given text
find the first number and check/loop if following chars are numbers
if following chars are numbers treat as one number else pass the
result
continue searching from last point and add the result to the total
in code:
Public Function ADD_NUMB(iText As String) As Long
Dim I, J As Integer
Dim T As Long
Dim TM As String
For I = 1 To Len(iText)
If (InStr(1, "12346567890", Mid$(iText, I, 1)) >= 1) Then
TM = Mid(iText, I, 1)
For J = I + 1 To Len(iText)
If (InStr(1, "12346567890", Mid$(iText, J, 1)) >= 1) Then
TM = TM & Mid$(iText, J, 1)
Else
Exit For
End If
Next J
T = T + Val(Nz(TM, 0))
I = J
End If
Next I
ADD_NUMB = T
End Function
usage:
dim total as integer
total = ADD_NUMB("21 F/C and 1 other")
not sure about performance but it will get you what you need :)