I have a config file that looks like this:
...
[env.staging]
name = "something"
...
[env.production]
name = "something"
...
I'm trying to replace the value of name on a specific environment using regex in the sed command in bash, but when I try to find the line by its section followed by a New Line, it doesn't work:
sed -i -e '/\[env\.production\]\nname =/s/=.*/= \"something_else\"/' config.toml
But the following command works fine, and of course, changes the name variable of both environments which is not desired.
sed -i -e '/name =/s/=.*/= \"something_else\"/' config.toml
Any ideas on how to achieve the correct result? (All my files are using LF line endings)
sed works line by line by default. You cannot match across multiple lines unless you use features to bring in multiple lines to the pattern space.
$ sed '/\[env\.production]/ {n; s/=.*/= "something_else"/}' config.toml
...
[env.staging]
name = "something"
...
[env.production]
name = "something_else"
...
n command will replace the pattern space with the next line. Use N when you need to process both lines together.
{} is used to group commands.
I would also suggest to use a toml tool like dasel instead of sed for such cases.
I've created some additional sample data for demonstration purposes:
$ cat config.toml
...
[env.staging]
id = 1
name = "something"
name2 = "something"
date = 2021/08/03
...
[env.production]
id = 2
name = "something"
name2 = "something"
date = 2022/01/23
...
[env.test]
id = 3
name = "something"
name2 = "something"
date = 2021/11/15
...
One sed idea using a range to find the desired section and then apply the change to said section:
$ sed '/\[env.production\]/,/^name = ".*$/ s/^name = ".*$/name = "something_else"/' config.toml
...
[env.staging]
id = 1
name = "something"
name2 = "something"
date = 2021/08/03
...
[env.production]
id = 2
name = "something_else"
name2 = "something"
date = 2022/01/23
...
[env.test]
id = 3
name = "something"
name2 = "something"
date = 2021/11/15
...
NOTES:
we don't know what the rest of the config sections look like so I went a bit overboard by specifying the entire line (^name = ".*$)
once OP is satisfied with the answer the -i option can be added to perform the 'in place' update of config.toml
Related
I've been using python-docx to produce large documents full of tables and figures conforming with a standard template. I have discovered how to make them cross-referenceable using https://github.com/python-openxml/python-docx/issues/359 . However this labels my figures/tables starting at 1 within each section and continuing until the next section where it restarts from 1.
I would like the figure numbers to be dependent on the section number (i.e. 1st figure in 2nd section = Figure 2.1 etc.). Does anyone know if this is possible?
Currently the numbering is produced by the function:
def Table(paragraph):
from docx.oxml import OxmlElement
from docx.oxml.ns import qn
run = run = paragraph.add_run()
r = run._r
fldChar = OxmlElement('w:fldChar')
fldChar.set(qn('w:fldCharType'), 'begin')
r.append(fldChar)
instrText = OxmlElement('w:instrText')
instrText.text = ' SEQ TableMain \* ARABIC \s 1 '
print instrText
r.append(instrText)
fldChar = OxmlElement('w:fldChar')
fldChar.set(qn('w:fldCharType'), 'end')
r.append(fldChar)
Called by the following code which also populates the table and table title and footer
table3 = document.add_table(rows=1, cols=1)
table3.cell(0,0).text="Table "
for paragraph in table4.cell(0,0).paragraphs:
paragraph.style = document.styles['Caption']
Table(paragraph)
paragraph.add_run(text="this is the full table name")
row_cells = table3.add_row().cells
call_func_that_makes_actual_table(row_cells[0],...)
row_cells = table3.add_row().cells
row_cells[0].text="Source: ..."
for paragraph in row_cells[0].paragraphs:
paragraph.style = document.styles['Source']
This produces a table like
this
Whereas I would like the table numbering like
this
Managed to work this out myself the solution is adding a further function:
def section(paragraph):
from docx.oxml import OxmlElement
from docx.oxml.ns import qn
run = run = paragraph.add_run()
r = run._r
fldChar = OxmlElement('w:fldChar')
fldChar.set(qn('w:fldCharType'), 'begin')
r.append(fldChar)
instrText = OxmlElement('w:instrText')
instrText.text = ' STYLEREF 1 \s '
r.append(instrText)
fldChar = OxmlElement('w:fldChar')
fldChar.set(qn('w:fldCharType'), 'end')
r.append(fldChar)
and changing the call to:
for paragraph in table.cell(1,0).paragraphs:
paragraph.style = document.styles['Caption']
section(paragraph)
paragraph.add_run(text=".")
Figure(paragraph)
paragraph.add_run(text=": this is the full table name")
This is what I wrote to get output with powercli;
Get-VM -name SERVERX | Get-Annotation -CustomAttribute "Last EMC vProxy Backup"|select #{N='VM';E={$_.AnnotatedEntity}},Value
This is the output
VM Value
-- -----
SERVERX Backup Server=networker01, Policy=vmbackup, Workflow=Linux_Test_Production, Action=Linux_Test_Production, JobId=1039978, StartTime=2018-10-31T00:00:27Z, EndTime=2018-10-31T00:12:45Z
SERVERX1 Backup Server=networker01, Policy=vmbackup, Workflow=Linux_Test_Production, Action=Linux_Test_Production, JobId=1226232, StartTime=2018-12-06T00:00:29Z, EndTime=2018-12-06T00:0...
SERVERX2 Backup Server=networker01, Policy=vmbackup, Workflow=Linux_Test_Production, Action=Linux_Test_Production, JobId=1226239, StartTime=2018-12-05T23:58:27Z, EndTime=2018-12-06T00:0...
But I would like retrieve only "starttime" and "endtime" values
Desired output is;
VM Value
-- -----
SERVERX StartTime=2018-10-31T00:00:27Z, EndTime=2018-10-31T00:12:45Z
SERVERX1 StartTime=2018-12-06T00:00:29Z, EndTime=2018-1206T00:11:14Z
SERVERX2 StartTime=2018-12-05T23:58:27Z, EndTime=2018-12-06T00:11:20Z
How can I get this output?
This would be better suited in Powershell forum as this is just data manipulation.
Providing your output is always the same number of commas then
$myannotation = Get-VM -name SERVERX | Get-Annotation -CustomAttribute "Last EMC
vProxy Backup"|select #{N='VM';E={$_.AnnotatedEntity}},Value
$table1 = #()
foreach($a in $myannotation)
$splitter = $a.value -split ','
$splitbackupstart = $splitter[5]
$splitbackupend = $splitter[6]
$row = '' | select vmname, backupstart, backupend
$row.vmname = $a.AnnotatedEntity # or .vm would have to try
$row.backupstart = $splitbackupstart
$row.backupend= $splitbackupend
$table1 += $row
}
$table1
Untested. If you format of the string is going to change over time then a regex to search for starttime will be better.
Sorry for previous confusion...
I've spent several hours today trying to write a powershell script that will pull a client ID off a PDF from system #1 (example, Smith,John_H123_20171012.pdf where the client ID is the H#### value), then look it up in an Excel spreadsheet that contains the client ID in system 1 and system 2, then rename the file to the format needed for system 2 (xxx_0000000123_yyy.pdf).
One gotcha is that client # is 2-4 digits in system 2 and always preceeded by 0's.
Using Powershell and regular expressions.
This is the first part I am trying to use for my initial rename:
Get-ChildItem -Filter *.pdf | Foreach-Object{
$pattern = "_H(.*?)_2"
$OrionID = [regex]::Match($file, $pattern).Groups[1].value
Rename-Item -NewName $OrionID
}
It is not accepting "NewName" because it states it is an empty string. I have run:
Get-Variable | select name,value,Description
And new name shows up as a name but with no value. How can I pass the output from the Regex into the rename?
Run this code line by line in debugger, you will understand how this works.
#Starts an Excel process, you can see Excel.exe as background process
$processExcel = New-Object -com Excel.Application
#If you set it to $False you wont see whats going on on Excel App
$processExcel.visible = $True
$filePath="C:\somePath\file.xls"
#Open $filePath file
$Workbook=$processExcel.Workbooks.Open($filePath)
#Select sheet 1
$sheet = $Workbook.Worksheets.Item(1)
#Select sheet with name "Name of some sheet"
$sheetTwo = $Workbook.Worksheets.Item("Name of some sheet")
#This will store C1 text on the variable
$cellString = $sheet.cells.item(3,1).text
#This will set A4 with variable value
$sheet.cells.item(1,4) = $cellString
#Iterate through all the sheet
$lastUsedRow = $sheet.UsedRange.Rows.count
$LastUsedColumn = $sheet.UsedRange.Columns.count
for ($i = 1;$i -le $lastUsedRow; $i++){
for ($j = 1;$j -le $LastUsedColumn; $j++){
$otherString = $sheet.cells.item($i,$j).text
}
}
#Create new Workbook and add sheet to it
$newWorkBook = $processExcel.Workbooks.Add()
$newWorkBook.worksheets.add()
$newSheet = $newWorkBook.worksheets.item(1)
$newSheet.name="SomeName"
#Close the workbook, if you set $False it wont save any changes, same as close without save
$Workbook.close($True)
#$Workbook.SaveAs("C:\newPath\newFile.xls",56) #You can save as the sheet, 56 is format code, check it o internet
$newWorkBook.close($False)
#Closes Excel app
$processExcel.Quit()
#This code is to remove the Excel process from the OS, this does not always work.
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($processExcel)
Remove-Variable processExcel
I ended up using a utility called "Bulk Rename Utility" and Excel. I can run the various renaming regex's through BRU and add the reference .txt file after some Excel formatting.
I am trying to filter out a group of lines that match a pattern using a regexp but am having trouble getting the correct regexp to use.
The text file contains lines like this:
transaction 390134; promote; 2016/12/20 01:17:07 ; user: build
to: DEVELOPMENT ; from: DEVELOPMENT_BUILD
# some commit comment
/./som/file/path 11745/409 (22269/257)
# merged
version 22269/257 (22269/257)
ancestor: (22133/182)
transaction 390136; promote; 2016/12/20 01:17:08 ; user: najmi
to: DEVELOPMENT ; from: DEVELOPMENT_BUILD
/./some/other/file/path 11745/1 (22269/1)
version 22269/1 (22269/1)
ancestor: (none - initial version)
type: dir
I would like to filter out the lines that start with "transaction", contain "User: build all the way until the next line that starts with "transaction".
The idea is to end up with transaction lines where user is not "build".
Thanks for any help.
If you want only the transaction lines for all users except build:
grep '^transaction ' test_data| grep -v 'user: build$'
If you want the whole transaction record for such users:
awk '/^transaction /{ p = !/user: build$/};p' test_data
OR
perl -lne 'if(/^transaction /){$p = !/user: build$/}; print if $p' test_data
The -A and -v options of grep command would have done the trick if all transaction records had same number of lines.
I'm trying to execute a command via ssh, take only the first part of the returned command, and set it as a variable to be used. I'm attempting to use RegExp and because I only need the first 4 digits, I use the pattern code "^\d{1,4}" I can successfully get the entire return via this code;
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec ("ssh -un -pw command")
Do Until oExec.StdOut.AtEndOfStream
ID = oExec.StdOut.ReadLine
Loop
WScript.Echo ID
But Now when I try to use RegExp and echo to see if I'm getting what I want I get a "Type Mismatch" error
Set RegExp = New RegExp
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec ("ssh -un -pw command")
RegExp.Pattern = "^\d{1,4}"
Do Until oExec.StdOut.AtEndOfStream
ID = oExec.StdOut.ReadLine
WScript.Echo RegExp.execute(ID)
Loop
If anyone has any incite on what's wrong with the code please let me know. If you know of any alternative I'd appreciate it. Using a shell script I can get what I'm looking for but I need to run this VBS with windows. Here's the Shell script if anyone can translate it to VBS.
.............for i in `command | sed ā1dā | awk ā{print $1}ā`..............
Appreciate any help in advance... Been racking my brain on this one for a while and getting burnt out.
You can't .Echo an object like the match collection you get from .Execute; access the .Value of its first item:
>> sInp = "1234xxx"
>> Set reHead = New RegExp
>> reHead.Pattern = "^\d{4}"
>> Set oMTS = reHead.Execute(sInp)
>> WScript.Echo oMTS(0).Value
>>
1234
>> WScript.Echo oMTS
>>
Error Number: 13
Error Description: Type mismatch