Find and Replace Multiple occurrences of a string within a large string - string

I have a large string.
I want to find a specific tag from the large string and replace content within that tag. This tag could occur multiple times and has to be replaced each time.
Below is my code:
$FirstString = '<tag>'
$lastString = '</tag>'
$contentVal = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. <tag> Aha Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes</tag>, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, <tag> Aha imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus.</tag> Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. <tag> Aha Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum.</tag>"
if ($contentVal.Contains('<tag>')) {
$Pos1 = $contentVal.IndexOf($FirstString) + $FirstString.Length;
$Pos2 = $contentVal.IndexOf($lastString);
#Find text between <tag> and </tag>
$FoundString = $contentVal.Substring($Pos1, $Pos2 - $Pos1);
Write-Host $FoundString
#Modify this FoundString by finding Aha and replacing with <br/>
$FinalString = $FinalString.Replace("Aha","</br>")
Write-Host $FinalString
}
Now I want to append this $finalString to $contentVal at the same position at which it was earlier.
Also, I have multiple occurences of <tag>.
How to manipulate each occurence and append to main string?

Related

how to construct a tkinter ttk textbox in Python

I am using ttk widgets for tkinter GUI.
I wonder how to construct a TextBox using a TTK widgets.
I can't find the equivalent TextBox widget as TTK
The Text widget is not available in ttk. Python - Text widget from tkinter in tkk
You can use Text. Here is an example:
from tkinter import *
r = Tk()
text = """Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque non ornare leo, nec pulvinar augue. Nam imperdiet sem eu quam placerat, placerat fermentum leo ornare. Sed malesuada non urna vel finibus. Vestibulum condimentum gravida nunc, faucibus malesuada augue laoreet sed. Pellentesque consectetur orci in felis lacinia finibus. Vestibulum luctus est quis velit hendrerit, et mattis neque pretium. Duis eget urna tristique, viverra ligula id, lacinia felis. Suspendisse potenti. Etiam nisi quam, tincidunt non risus a, rhoncus vulputate augue. Morbi molestie sagittis lectus, non lacinia eros pellentesque in.
Integer aliquet finibus sapien at feugiat. Fusce lobortis vulputate ex, vitae faucibus nunc eleifend eu. Pellentesque hendrerit tortor a est vulputate, vel porttitor eros pretium. Pellentesque rhoncus felis imperdiet, lacinia dolor sed, tempus eros. Duis placerat fringilla pulvinar. Maecenas erat nisl, consectetur ac ipsum sed, hendrerit fermentum leo. Suspendisse sed tortor ultricies lacus porttitor viverra. Aenean quis imperdiet nulla. Vivamus a sem tellus. Ut vitae neque a enim bibendum euismod."""
t = Text(r)
t.pack(fill=BOTH, expand=1)
t.insert("end", text)
r.mainloop()

Search from a string to another in Linux

I am trying to find a way to search from a string within a text to another string.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus massa nulla, lobortis sit amet placerat hendrerit, mollis quis nulla. Morbi consectetur, odio vel rhoncus euismod, nunc nisi euismod ante, vitae molestie ante nulla non est.
Vivamus eget fermentum lorem, sed suscipit nulla. Aliquam consequat ultrices maximus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
Etiam vitae tortor quis lectus convallis ullamcorper. Nullam nec dignissim tellus, vel dictum nisi. Etiam sit amet libero vulputate, eleifend libero nec, semper ex. Cras eu magna fringilla, iaculis sapien id, feugiat lorem. Ut id velit mauris.
I'd like to know if there is a way, in the command line, to come up with what's in between the bolded words in the paragraph above.
I've tried different variations of grep without any success.
You can use parameter substitution:
#!/bin/bash
string=$( <dat/lorem.txt )
tmp=${string#*amet}
tmp=${tmp%tellus*}
echo $tmp
output:
$ string=$( <dat/lorem.txt ); tmp=${string#*amet}; tmp=${tmp%tellus*}; echo $tmp
, consectetur adipiscing elit. Phasellus massa nulla, lobortis sit amet placerat hendrerit,
mollis quis nulla. Morbi consectetur, odio vel rhoncus euismod, nunc nisi euismod ante,
vitae molestie ante nulla non est. Vivamus eget fermentum lorem, sed suscipit nulla.
Aliquam consequat ultrices maximus. Pellentesque habitant morbi tristique senectus et
netus et malesuada fames ac turpis egestas. Etiam vitae tortor quis lectus convallis
ullamcorper. Nullam nec dignissim
by one sed command
sed -n ':a;$!{N;ba};s/.*\(amet.*tellus\).*/\1/p' infile
amet placerat hendrerit, mollis quis nulla. Morbi consectetur, odio vel rhoncus euismod, nunc nisi euismod ante, vitae molestie ante nulla non est.
Vivamus eget fermentum lorem, sed suscipit nulla. Aliquam consequat ultrices maximus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
Etiam vitae tortor quis lectus convallis ullamcorper. Nullam nec dignissim tellus
If you needn't the keywords in output:
sed -n ':a;$!{N;ba};s/.*amet\(.*\)tellus.*/\1/p' infile
placerat hendrerit, mollis quis nulla. Morbi consectetur, odio vel rhoncus euismod, nunc nisi euismod ante, vitae molestie ante nulla non est.
Vivamus eget fermentum lorem, sed suscipit nulla. Aliquam consequat ultrices maximus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
Etiam vitae tortor quis lectus convallis ullamcorper. Nullam nec dignissim
Use sed for this one :
sed -n '/amet/,/tellus/p' atext.txt | sed 's/.*amet/amet/;s/tellus.*/tellus/'
Output should look like:
amet ...(everything in between)...tellus
The first sed deletes all lines except the ones that include the words amet and tellus, and everything in between.
The second sed deletes all words before amet, and all words after tellus
You can use a regular expression
grep -e 'amet .* tellus' yourfile
Where . matches any character except line breaks, and * means 0 or more times.

Using par to format stylized Fortran comments

All,
I was wondering if anyone out there was a par guru. I'm mainly wondering if there is a way to get par to see a Fortran comment style that is used in many programs I work with.
For example, if I have:
! 2001Jan01 Jimson Lorem ipsum dolor sit amet, consectetuer adipiscing
! elit. Aenean commodo ligula eget dolor.
! 2002Jan01 Johnny Aenean massa. Cum sociis natoque penatibus et magnis dis parturient
! montes, nascetur ridiculus mus. Donec quam felis, ultricies nec,
! pellentesque eu, pretium quis, sem.
! 2004Feb01 Thompson Nulla consequat massa quis enim. Donec pede justo, fringilla vel,
! aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo.
! Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus.
It would be nice to run par in vim (with :set formatprg=par) and get something like:
! 2001Jan01 Jimson Lorem ipsum dolor sit amet, consectetuer adipiscing
! elit. Aenean commodo ligula eget dolor.
! 2002Jan01 Johnny Aenean massa. Cum sociis natoque penatibus et magnis
! dis parturient montes, nascetur ridiculus mus. Donec
! quam felis, ultricies nec, pellentesque eu, pretium
! quis, sem.
! 2004Feb01 Thompson Nulla consequat massa quis enim. Donec pede justo,
! fringilla vel, aliquet nec, vulputate eget, arcu. In
! enim justo, rhoncus ut, imperdiet a, venenatis vitae,
! justo. Nullam dictum felis eu pede mollis pretium.
! Integer tincidunt. Cras dapibus.
where it sees the hanging spaces, etc. Instead par does:
(524) $ par -w80 < text_for_par
! 2001Jan01 Jimson Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
! Aenean commodo ligula eget dolor. 2002Jan01 Johnny Aenean massa. Cum sociis
! natoque penatibus et magnis dis parturient montes, nascetur ridiculus
! mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem.
! 2004Feb01 Thompson Nulla consequat massa quis enim. Donec pede justo,
! fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus
! ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis
! pretium. Integer tincidunt. Cras dapibus.
which, I do admit, is quite well formatted at 80 characters and keeps the "!<space><space>" at the front.
Unfortunately, par's PARINIT syntax is a bit...mysterious to me. It is entirely possible this is not possible and, if so, okay. But par seems to be powerful in a way like vim is. As long as you know the right confusing character string, it'll do what you want.
I'm not a par expert but if you try something like
cat text_for_par | par 80 -p24 -d1
it should give you what you're looking for. Just look at the man page on par - it tells you how to achieve most things.

VIM Spellcheck, Within Quoted Strings - PHP

I was wondering if it would be possible to run the vim spell checker while coding with PHP.
I can run it if I disable syntax highlighting, and it highlights almost all methods/function names because they are not English words.
So, my question is this, can I run the spell check on just the strings within PHP files?
For example, just within the ' quotes (and " quotes), ignoring everything else:
$paragraph = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. In est libero, dictum ut suscipit eget, lacinia in justo. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Praesent sit amet sem libero, in pretium enim. Pellentesque tortor ante, imperdiet quis mattis in, tincidunt et ligula. Cras porta velit a ligula venenatis placerat.';
$paragraph2 = "Vestibulum quis eleifend lectus. Vestibulum odio odio, mollis at eleifend a, adipiscing sed quam. Nam quis nisi quis mauris faucibus hendrerit eu a leo. Sed feugiat purus sit amet purus congue quis semper diam malesuada. Vivamus et enim non arcu pretium sollicitudin. Nullam blandit, lorem eu aliquet tincidunt, enim mauris placerat lectus, id faucibus nulla lectus non elit. Sed eget fermentum lacus. Ut tempus pulvinar neque, vel pellentesque purus aliquet quis. Vivamus molestie pretium quam at imperdiet. Maecenas diam turpis, malesuada ut sollicitudin ac, congue eleifend urna. Quisque id ipsum eget ipsum tempus blandit at vel magna. Fusce eu felis metus.";
see the vim help: :help spell-syntax
You can define particular syntax groups as having spell checking.
There's probably a better way to do it (using a ~/.vim/after/syntax/php.vim file?), but a simple hack is to make a personal version of the php syntax file in ~/.vim/syntax (you can do this by doing :e $VIMRUNTIME/syntax/php.vim and the :saveas ~/.vim/syntax/php.vim), and change all instances of the text contains=#phpAddStrings to contains=#Spell,#phpAddStrings. Next time you open a php file, turn on spell checking as normal with :set spell.

Using vim's `gqap' sometimes indents unusually

For the life of me, I cannot figure out the cause of this: when writing in LaTeX documents, I like to keep my line width to maximum of 80 characters. As such, I will execute the vim command gqap and vim will automatically re-wordwrap the paragraph I am writing.
For example, it will cause a long line to become many shorter ones:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque pharetra nunc eget arcu dapibus pretium. Nulla vel risus quam, ut sollicitudin sem. Vivamus vitae diam in risus pharetra gravida. Donec rutrum mattis nulla, in consectetur lorem luctus varius. Donec augue purus, iaculis eget fringilla nec, vehicula ut sapien. Quisque sit amet dolor mauris. Sed ac est eu ligula aliquam tincidunt. Proin condimentum rutrum lacinia.
becomes:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque pharetra
nunc eget arcu dapibus pretium. Nulla vel risus quam, ut sollicitudin sem.
Vivamus vitae diam in risus pharetra gravida. Donec rutrum mattis nulla, in
consectetur lorem luctus varius. Donec augue purus, iaculis eget fringilla nec,
vehicula ut sapien. Quisque sit amet dolor mauris. Sed ac est eu ligula aliquam
tincidunt. Proin condimentum rutrum lacini
But, for some paragraphs, it will start adding unusual indentions towards the end of the paragraph:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque pharetra
nunc eget arcu dapibus pretium. Nulla vel risus quam, ut sollicitudin sem.
Vivamus vitae diam in risus pharetra gravida. Donec rutrum mattis nulla, in
consectetur lorem luctus varius. Donec augue purus, iaculis eget fringilla,
vehicula ut sapien. Quisque sit amet dolor mauris. Sed ac est eu ligula
tincidunt. Proin condimentum rutrum lacini
Why does vim do this? I have yet to figure out the pattern. How can I make it format paragraphs "the correct way"?
Yes, vim's 'smartindent' is not well named. I had trouble with this for a while before I figured out what was wrong. For others, if you notice strange indenting behavior after lines starting with "if" or "for", unset smartindent. You will come across this a lot if you start using vim as a word processor.
From the vim help file, 'smartindent' will indent after a line starting with a keyword from 'cinwords', which is "if,else,while,do,for,switch", by default.
The problem was that I had "smartindent" enabled. Disabling this for LaTeX documents solved it:
au BufEnter *.tex set nosmartindent

Resources