In awk, we can express patterns in various ways, e. g. as regular expressions enclosed in delimiters /.../ or as a boolean expression where a non-null value (string) or a nonzero value (number) is true. I however have this pattern and I'm wondering what it does:
/test/p
It prints all lines, whatever value p would have and whatever regexp is provided. How is this pattern to be read? In the documentation I didn't find anything about this. I'd expect an error message like when the ending delimiter is missing. (I know it's a common expression for sed.)
The /test/ evaluates to 0 or 1, depending on whether the current record (line) matches the regular expression.
It is string-concatenated with the result of evaluating the variable p (probably an empty string).
So you end up with a condition of either "0" or "1", both of which are true, which means that the default action { print } is executed.
The fact that the p looks like a regex modifier is just a coincidence; the same behaviour can be observed with any of the following, which all produce a non-empty string (p is assumed to be empty):
/test/ p # concatenate 0 or 1 with empty variable
/test/ "" # concatenate 0 or 1 with empty string literal
0 p # "0"
0 "" # "0"
Related
my #r = split("", "hi");
say #r.elems;
--> output: 4
split is adding two extra elements to the array, one at the beginning and another at the end.
I have to do shift and pop after every split to correct for this.
Is there a better way to split a string?
If you're splitting on the empty string, you will get an empty element at the start and the end of the returned list as there is also an empty string before and after the string.
What you want is .comb without parameters, written out completely functionally:
"hi".comb.elems.say; # 2
See https://docs.raku.org/routine/comb#(Str)_routine_comb for more info.
The reason for this is when you use an empty Str “” for the delimiter it is the same as if you had used the regex /<|wb>/ which matches next to characters. So it also matches before the first character, and after the last character. Perl 5 removes these “extra” strings for you in this case (and in this case only), which is likely where the confusion lays.
What Perl 6 does instead is allow you to explicitly :skip-empty values
'hi'.split('') :skip-empty
'hi'.split('', :skip-empty)
split("", "hi") :skip-empty
split("", "hi", :skip-empty)
Or to specify what you actually want
'hi'.comb( /./ )
'hi'.comb( 1 )
'hi'.comb
comb( /./, 'hi' )
comb( 1, 'hi' )
I am actually using conga composer with word template to generate word doc fill with data from my database. There is a section where I have to display from a loop a single line that meet a requirement. This also works fine, but when looping, for each line that do not match the requirement words is leaving a blank line. I don't want blank lines to appear :
Here is my IF condition :
A : {{TableStart:TiersPrestataires}}
{ IF "<<variable>>" = "S1" "<<variable1>> <<variable2>>" ""}
{{TableEnd:TiersPrestataires}}
If in my TiersPrestataires variables I have 5 lines , with the 3rd line that respect the condition, this will print :
A :
<blank_line>
<blank_line>
some text represented by variable 1 some text represented by variable2
<blank_line>
<blank_line>
I want to print :
A:
some text represented by variable 1 some text represented by variable2
and that's all.How can I prevent word to replace the unmet condition with a blank line ? Is there something I can specify in my else condition?
It will be the way you position your brackets.
So I am assuming is another conditional, therefore you would do it like:
A:
{ IF (CONDITION 1) "true
"}{ IF (CONDITION 2) "true
"}{ IF "<<variable>>" = "S1" "<<variable1>> <<variable2>>
"}{ IF (CONDITION 4) "true
"}{ IF (CONDITION 5) "true
"}
If you only want to show content when condition is met then you only need to do one set of " " after the condition as false will be blank anyone.
Doing it this way ensures there are no blank lines left from the IF.
Hope this helps.
A colleague has some data he is putting into a flat file (.txt) and needs to insert a carriage return before EACH occurrence of 'POL01', 'SUB01','VEH01','MCO01'.
I did use:
For Each line1 As String In System.IO.File.ReadAllLines(BodyFileLoc)
If line1.Contains("POL01") Or line1.Contains("SUB01") Or line1.Contains("VEH01") Or line1.Contains("MCO01") Then
Writer.WriteLine(Environment.NewLine & line1)
Else
Writer.WriteLine(line1)
End If
Next
But unfortunately it turns out that the file is not formatted in 'lines' by SSIS but as one whole string.
How can I insert a carriage return before every occurrence of the above?
Test Text
POL01CALT302276F 332 NBPM 00101 20151113201511130001201611132359 2015111300010020151113000100SUB01CALT302276F 332 NBPMP01 Akl Abi-Khalil 19670131 M U33 Stoford Close SW19 6TJ 2015111300010020151113000100VEH01CALT302276F 332 NBPM001LV56 LEJ N 2006VAUXHALL CA 2015111300010020151113000100MCO01CALT302276F 332 NBPM0101 0 2015111300010020151113000100POL01CALT742569N
You can use regular expressions for this, specifically by using Regex.Replace to find and replace each occurrence of the strings you're looking for with a newline followed by the matching text:
Dim str as String = "xxxPOL01xxxSUB01xxxVEH01xxxMCO01xxx"
Dim output as String = Regex.Replace(str, "((?:POL|SUB|VEH|MCO)01)", Environment.NewLine + "$1")
'output contains:
'xxx
'POL01xxx
'SUB01xxx
'VEH01xxx
'MCO01xxx
There may be a better way to construct this regular expression, but this is a simple alternation on the different letters, followed by 01. This matched text is represented by the $1 in the replacement string.
If you're new to regular expressions, there are a number of tools that help you understand them - for example, regex101.com will show you an explanation of the one I have used here:
Please help me with 2 questions on how to do the GREL expression for:
If there are double spaces between 2 words in a column, how can I eliminate 1 space Example: Robert--Smith to Robert-Smith The minus character equals a blank for illustration
How can I look for an exact word in a text filter.
Thanks!
1°) try transform---> value.replace(" "," ")
Or, simply common transforms ----> collapse consecutive white spaces
2°) Column ---> text filters and enter you word
Or, do column---> Facet---> Customs facet and type : value.contains(" you_word ")
or value.contains(/(yourexactword)/)
This will return a True or False facet
H.
#hpiedcoq is the right answer if you need to have them in GREL. if not you can just use the point and click interface:
for the first question: Select your column and select Edit cells > Common transforms > Collapse consecutive white space
for the second question: select your column > text filter > enter the work you are looking for. You can select case sensitive if you want to take into account upper and lower case in your search.
1.1 transform -- > value.replace(" "," ")
Deletes all double whitespace.
1.2 transform -- > value.trim()
Deletes all double whitespace and deletes whitespaces before and after the string.
1.3 transform -- > value.replace(/\b \b/," ")
Replace with regular expression, deletes only double whitespace between two words.
Text filter > turn on regular expression and use \b.
Text filter with regular expression: \bWord\b = exact word, before and after the word may or may not be a only whitespace.
I need to count how often a number is present in a string. it should count EVERY occurence with a whitespace in front, except those followed by a =.
For example:
If i need to know how many "1" there are in this string: this is a 1 ramdnom string with 2 numbers 1 with 1=something it should return 2, as the third one is followed by an =
To find the occurrences I am using this: occurences = mystring.Split(" 1").Length - 1
But how to exclude those followed by a =?
Thanks
Something like,
Dim occurrences = Regex.Matches(yourString, "\W[0-9]([^=]|$)").Count
If you'd like to do replacements, use a Regex.Replace overload.
Breaking it down, this expression matches
\W // any whitespace character
[0-9] // any deciaml digit
( // either
[^=] // not =
| // or
$ // the end of the string
)