I typed these content in vimwiki :
here $1 is matched for[[name]]
and then I convert wiki to HTML :
here \)1 is matched for name
The question is how to get true value of specail character ,such as $, [[ ?
in Regexp , I can use backslash to use tre value of a character :
here \$1 ....
But in vimwiki , how can I do it ?
Related
My task is to write a .sh script that will load the user's first name. Then it will use a loop to count the occurrences of the letter 'a' and then print their number.
I understand that it is loading the text:
read / p "Please enter some text" text
Only then referring to the element $ {text [0]} gets all the text, not its single element
#!/bin/bash
echo "Please write"
read b
if [ ${b:${#b}-1:1} -eq 'a' ] ; then
echo "Women"
else
echo "man"
fi
l=0
for (( i=0 ; i< ${#b} ; i++ )) do
if [ ${b:$i:1} -eq 'a' ] ; then
((l++))
fi
done
echo L = $l
For counting the number of a characters in a variable, you could erase first all characters which are not an a. Example:
text=abcaagg
atext=${text//[!a]/}
The variable atext now holds only aaa. Calculate the length of that string, and you know how many a you had in your original variable:
echo ${#atext}
UPDATE: By request, I quote here the part of the bash man page which eplains the substitution. It is stated in the section titled Parameter Expansion:
${parameter/pattern/string}
Pattern substitution. The pattern is expanded to produce a pattern
just as in pathname expansion. Parameter is expanded and the long‐
est match of pattern against its value is replaced with string. If
pattern begins with /, all matches of pattern are replaced with
string. Normally only the first match is replaced. If pattern be‐
gins with #, it must match at the beginning of the expanded value
of parameter. If pattern begins with %, it must match at the end
of the expanded value of parameter. If string is null, matches of
pattern are deleted and the / following pattern may be omitted. If
the nocasematch shell option is enabled, the match is performed
without regard to the case of alphabetic characters.
I have to replace everything between two strings in a file but only until the first occurrence of the second string.
Here is a sample line of file content,
AuthorSessionId
, CONVERT_TZ(CreatedAt,"${DatabaseTimezone}","${ApplicationTimezone}") AS CreatedAt
FROM PlayerPhoneArchive
WHERE RevisionDate BETWEEN ? AND ?
) AS PH
In this, I wanted to replace everything starts from CONVERT_TZ until before the first of
with CreatedAt
.
The final output should be,
AuthorSessionId
, CreatedAt
FROM PlayerPhoneArchive
WHERE RevisionDate BETWEEN ? AND ?
) AS PH
Here is what I tried,
sed -e 's/CONVERT_TZ.*

/CreatedAt\
\
/
But It is replacing until the last occurrence of

, i.e., the end of string and coming as,
AuthorSessionId
, CreatedAt
as it is replacing until the last occurrence of the
.
How to replace only until the first occurrence of string
? Any help on this is greatly appreciated.
The greedy issue can be avoided in this particular case by adding CreatedAt to search term
sed 's/CONVERT_TZ.*CreatedAt

/CreatedAt\
\
/'
# or use back-reference to re-use strings
sed 's/CONVERT_TZ.*\(CreatedAt

\)/\1/'
In general, you can use non-greedy .*? if the regex flavor used by command supports it
perl -pe 's/CONVERT_TZ.*?

/CreatedAt

/'
# or lookahead to avoid repeating '

'
perl -pe 's/CONVERT_TZ.*?(?=

)/CreatedAt/'
my $book = Spreadsheet::Read->new();
my $book = ReadData
('D:\Profiles\jmahroof\Desktop\Scheduled_Build_Overview.xls');
my $cell = "CD7";
my $n = "1";
my $send = $book->[$n]{$cell};
$send =~ s/\(/ /g;
$send =~ s/\)//g;
I have the above code that gets data from an excel file and then picks out text from a specified cell and removes brackets from the string. I need to be able to remove everything within the brackets including the brackets themselves while leaving the rest of the text. The format of the string is exactly like the following : text(text)
$send =~ s/\(.*?\)//;
Explained:
s/ does the search
\ escapes the bracket that comes next as it will be seen as part of the code if not escaped.
(.*?\) here we say what we are searching for an we use non-greedy .*? to match anything up to the last bracket again the last bracket is escaped by \
/ begins the replace function for the search
/ ends the search and replace.
So we Search for (*) and then replace with nothing.
Explaing Non greedy vs Greedy.
.* being greedy will match up until the last ) found
So if we have string((substring)end) then s/(.*)// will go from the first ( up to the last ) leaving you with string
non greedy will not, it will begin with the first ( up to the first ) leaving you with stringend) so it will be lazy and only match what you ask for which is from ( to ) where greedy will match grab everything, even if you have this (string)(())((strings))()()strings)strings)
If you don't have nested parentheses, this single substitution can do it:
$send =~ s/\(.*?\)//;
If these parenthesis are always the last item in text, it can be further simplified to:
$send =~s/\(.*//;
I want to search the following string in a text file: $$u$$
I tried select-string, get-content, .Contains. It seems to me it's not possible.
I used this for the search as a variable: $ToSearch = "'$'$u'$'$"
It always gives false result.
It is because most search filters are relying on regex. The $ symbol in regex needs to be escaped
Without knowing more of what you're trying to accomplish I can't give much of an example, but here is one:
'this is a $$u$$ test' -replace "\$",""
The '\' is what is escaping the character - meaning to translate it literally.
Edit: Per comment
$Val = 'this is a $$u$$ test'
$Val | Select-String "\$+\w\$+" -quiet
-Quiet switch returns t/f rather than a string value.
The $ is a reserved character in regex, which is complicating your search.
The default search pattern type for Select-String is regex, but you can also specify -Simplematch or -Wildcard, either of which will eliminate the need to escape the $. If you use -Wildcard, you'll need to include the wilcard * at either end of the match - '*$$u$$*'. For simplematch, just use the string you want to match for - '$$u$$'.
In my shellscript code I saw that there is line that is handling Telephone number using sed command.
sed "s~<Telephone type[ ]*=[ ]*\"fax\"[ ]*><Number>none[ ]*</Number></Telephone>~~g" input.xml > output.xml
I am not understanding what the regular expression actually does.
<Telephone type[ ]*=[ ]*\"fax\"[ ]*><Number>none[ ]*</Number></Telephone>
I am doing revere engineering to get this working.
My xml structure like below.
<ContactMethod>
<InternetEmailAddress>donald.francis#lexisnexis.com</InternetEmailAddress>
<Telephone type = "work">
<Number>215-639-9000 x3281</Number>
</Telephone>
<Telephone type = "home">
<Number>484-231-1141</Number>
</Telephone>
<Telephone type = "fax">
<Number>N/A</Number>
</Telephone>
<Telephone type = "work">
<Number>215-639-9000 x3281</Number>
</Telephone>
<Telephone type = "home">
<Number>484-231-1141</Number>
</Telephone>
<Telephone type = "fax">
<Number>none</Number>
</Telephone>
<Telephone type1 = "fax12234">
<Number>484-231-1141sadsadasdasdaasd</Number>
</Telephone>
</ContactMethod>
That regex recognises <Telephone type = "fax"> entries where the number is given as none, and deletes them.
Breakdown:
s sed command for "substitution".
~ pattern separator. You can choose any character for this. sed recoginizes it because it comes right after the s.
<Telephone type This matches the literal text "<Telephone type".
[ ]* matches zero or more spaces.
= matches a literal "="
[ ]* matches zero or more spaces.
\"fax\" matches literal text. The quotes are escaped because the whole pattern appears inside quotes, but the shell removes the quote characters (\) before sed sees them.
[ ]* matches zero or more spaces.
><Number>none matches literal text.
[ ]* matches zero or more spaces.
</Number></Telephone> matches the literal text.
~~ the pattern separators end the search pattern, and surround an empty replace pattern.
g is a flag that means the substitution will be performed multiple times on each line.
The only thing that confuses me is that this pattern won't match anything that has line breaks in it, so I presume your input.xml isn't actually formatted like you have in your example data?