I am trying to read content of a file, after finding matching with content need to skip one more line & add some text.
For Ex: My text file contains:
cd $home/t17_0/download/functional-tests/
ant compile-all
cd $home/15_5/download/functional-tests/
ant compile-all
cd $home/15_7/download/functional-tests/
ant compile-all
I want match line with cd $home/15_5/download/functional-tests/
(line number 3) and add cd $home/15_6/download/functional-tests/
after ant compile-all (line number 4). Please note that in my real scenario i don't know the line number.
I am able to write script to add the text after matching the content as shown below:
$mat='cd $home/15_5/download/functional-tests/'
$add='cd $home/15_6/download/functional-tests/'
$file_content=get-content text.txt
($file_content) | foreach-object {
$_
if ($_ -contains $mat)
{
$add
}
}| set-content $file_content
This is matching line with cd $home/15_5/download/functional-tests/
(line number 3) and adds cd $home/15_6/download/functional-tests/
before ant compile-all
(line number 4).