How effectively search alternatives with rgrep in Emacs - emacs

I'm trying to find where in the code base of OpenERP both the names "default_debit_account_id" and "default_credit_account_id" are used.
If I search them separately they both yield more than 20 results. However if I try M-xrgrepRET\bdefault_(debit|credit)_account_id\bRET this time no results are retrived. I have tried also placing a single ("\") and double slashes ("\") before the parentheses, but it didn't help.
Ultimately this is the command run by Emacs
find . -type d \( -path \*/SCCS -o -path \*/RCS -o -path \*/CVS -o -path \*/MCVS -o \
-path \*/.svn -o -path \*/.git -o -path \*/.hg -o -path \*/.bzr -o \
-path \*/_MTN -o -path \*/_darcs -o -path \*/\{arch\} \) -prune -o \
\! -type d \( -name .\#\* -o -name \*.o -o -name \*\~ -o -name \*.bin -o -name \*.lbin -o -name \*.so -o -name \*.a -o -name \*.ln -o -name \*.blg -o -name \*.bbl -o -name \*.elc -o -name \*.lof -o -name \*.glo -o -name \*.idx -o -name \*.lot -o -name \*.fmt -o -name \*.tfm -o -name \*.class -o -name \*.fas -o -name \*.lib -o -name \*.mem -o -name \*.x86f -o -name \*.sparcf -o -name \*.dfsl -o -name \*.pfsl -o -name \*.d64fsl -o -name \*.p64fsl -o -name \*.lx64fsl -o -name \*.lx32fsl -o -name \*.dx64fsl -o -name \*.dx32fsl -o -name \*.fx64fsl -o -name \*.fx32fsl -o -name \*.sx64fsl -o -name \*.sx32fsl -o -name \*.wx64fsl -o -name \*.wx32fsl -o -name \*.fasl -o -name \*.ufsl -o -name \*.fsl -o -name \*.dxl -o -name \*.lo -o -name \*.la -o -name \*.gmo -o -name \*.mo -o -name \*.toc -o -name \*.aux -o -name \*.cp -o -name \*.fn -o -name \*.ky -o -name \*.pg -o -name \*.tp -o -name \*.vr -o -name \*.cps -o -name \*.fns -o -name \*.kys -o -name \*.pgs -o -name \*.tps -o -name \*.vrs -o -name \*.pyc -o -name \*.pyo \) -prune -o \
-type f \( -name \*.py \) \
-exec grep -i -nH -e \\bdefault_\(debit\|credit\)_account_id\\b {} +
Thus grep sees the pattern (after shell escaping is perfomed) \bdefault_(debit|credit)_account_id\b. What's wrong with this? How should I search this?

You need to use the Emacs flavor of regex: \bdefault_\(debit\|credit\)_account_id\b.

Related

Power shell and .bat file

Because im not so good in power shell, i wanted to ask how to manage this task. I have created .bat file which doing this:
echo before delete > delete.log
forfiles -p "C:\test1" -s -m *.* -d -0 -c "cmd /c echo #path" >> trace.log
forfiles -p "C:\test1" -s -m *.* -d -1 -c "cmd /c del #path" >> trace.log
echo preserved >> trace.log
forfiles -p "C:\test1" -s -m *.* -d -0 -c "cmd /c echo #path" >> trace.log
And output of that trace.log i send successfully via separate power shell script like this:
$body = Get-Content -Path "C:\test1\trace.log" -Raw
Send-MailMessage -from doNotReply#company.com -to "nobody#company.com" -subject "Trace log delete status test" -body $body -smtpServer smtp.company.com
Because of my limited knowledge in power shell is there a way to merge .bat commands with my current power shell script? Idea is to do action of delete like in above sample, create log and send it by email in one power shell script.
Try like this:
Write-Host "before delete" > delete.log
(Get-ChildItem "C:\test1" -Recurse |
Select-Object -ExpandProperty Fullname) >>trace.log
(Get-ChildItem "C:\test1" -Recurse |
Where-Object {$_.LastWriteTime -ge (Get-Date).addDays(-1)} |
Remove-Item -Force) >>trace.log
Write-Host "preserved" >> trace.log
(Get-ChildItem "C:\test1" -Recurse |
Select-Object -ExpandProperty Fullname) >>trace.log
$body = Get-Content -Path "C:\test1\trace.log" -Raw
Send-MailMessage -from doNotReply#company.com -to "nobody#company.com" -subject "Trace log delete status test" -body $body -smtpServer smtp.company.com
#marsze Ok sorry, so i run ISE like admin user and executed this line Set-ExecutionPolicy -Scope LocalMachine Unrestricted. Than i copied all above proposed code in same session and run, and i hit this errors:
Get-ChildItem : Access to the path 'C:\Program Files\Websense\Websense Endpoint\certutil' is denied.
At line:8 char:2
+ (Get-ChildItem "C:\test1" -Recurse |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\Program File...dpoint\certutil:String) [Get-ChildItem], Unauth
orizedAccessException
+ FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand
Get-ChildItem : Access to the path 'C:\Windows\CCM\ScriptStore' is denied.
At line:8 char:2
+ (Get-ChildItem "C:\test1" -Recurse |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\Windows\CCM\ScriptStore:String) [Get-ChildItem], UnauthorizedA
ccessException
+ FullyQualifiedErrorId :

Powershell script to list ,in reverse order, files recursively, similarly to this bash script

I am trying to convert this bash script to powershell. I'm doing this because Windows subsystem for Linux currently doesn't work on Google Drive File System's mounted drives so I need to use powershell.
Background
I need to run this powershell script on a top-level parent folder to import 87 million json files in ~700,000 deeply nested folders. I'm open to better ways of doing this.
My Bash Solution
#!/bin/sh
function import_from_start() {
echo "starting import from front"
find "$1" -name '*.json' | while read file; do
mongoimport --host=datalake7 --db=CA_facebook_copy --collection=test_import --type="json" --file="$file"
done
}
function import_from_end() {
echo "starting import from back end"
find "$1" -name '*.json' | sort -r | cut -f2 | while read file; do
mongoimport --host=datalake7 --db=CA_facebook_copy --collection=test_import --type="json" --file="$file"
done
}
import_from_start "$1" &
import_from_end "$1"
My current attempted powershell script
param (
[Parameter(Mandatory=$true)][string]$Src,
[Parameter(Mandatory=$true)][string]$Collection
)
$Extension = '*.json'
Get-ChildItem -Path $Src -Filter $Extension -Recurse | Where-Object {!$_.PsIsContainer} | ForEach-Object {
.\mongoimport.exe --host datalake7 --db CA_facebook_copy --collection $Collection --type json --file $_.FullName
}
My Goal
I need to import from the start and from the end of the file list at the same time. I'm using this approach because its very slow to create and destroy a connection to mongodb for each file.
Now sorting could be based on any column. So in your case, you can take the all files fullname.length since you have used fullname and can sort that in descending order.
Replace
Get-ChildItem -Path $Src -Filter $Extension -Recurse | Where-Object {!$_.PsIsContainer} | ForEach-Object {
.\mongoimport.exe --host datalake7 --db CA_facebook_copy --collection $Collection --type json --file $_.FullName
}
With this:
Get-ChildItem -Path $Src -Filter $Extension -Recurse | Where-Object {!$_.PsIsContainer} | ForEach-Object {
.\mongoimport.exe --host datalake7 --db CA_facebook_copy --collection $Collection --type json --file $_.FullName
}| Sort-Object #{expression = {$_.fullname.length}} -descending
I have added the sort object part after the foreach loop.
Hope it helps.

Finding the path & product version of a file in powershell

I am try to create Powershell script to search for a sacrifice file (.exe) on the same machine or another machine in the network and return all the paths of the files with the product version for each one
I used the below code but I only was able to get the path only
$filename = 'example.exe'
(gdr -PSProvider 'FileSystem' | %{ ls -r $_.root} 2>$null | where { $_.name -eq ".EXE" })
(gdr -PSProvider 'FileSystem' | %{ ls -r $_.root} 2>$null | where { $_.name -eq "$filename" } | Select FullName,#{n='Version';e={$_.versioninfo.productversion}})

PowerShell configuration with an rc-like file

What script and where I should write in order to define:
alias for ll="ls -l"
alias/function cd = "original cd; ll"
So, my question has to parts where is the rc files of the Power Shell on Windows 7 and how to alias ll to ls -l and cd to cd; ll?
Create a file in the place where the power shell points when you type $profile and press enter, if it does not exist. (For more info look here.)
Also I have found a lots of good examples next to the powershell.exe in my system there is an example folder, where there is a file named profile.ps1 with the following code:
set-alias cat get-content
set-alias cd set-location
set-alias clear clear-host
set-alias cp copy-item
set-alias h get-history
set-alias history get-history
set-alias kill stop-process
set-alias lp out-printer
set-alias ls get-childitem
set-alias mount new-mshdrive
set-alias mv move-item
set-alias popd pop-location
set-alias ps get-process
set-alias pushd push-location
set-alias pwd get-location
set-alias r invoke-history
set-alias rm remove-item
set-alias rmdir remove-item
set-alias echo write-output
set-alias cls clear-host
set-alias chdir set-location
set-alias copy copy-item
set-alias del remove-item
set-alias dir get-childitem
set-alias erase remove-item
set-alias move move-item
set-alias rd remove-item
set-alias ren rename-item
set-alias set set-variable
set-alias type get-content
function help
{
get-help $args[0] | out-host -paging
}
function man
{
get-help $args[0] | out-host -paging
}
function mkdir
{
new-item -type directory -path $args
}
function md
{
new-item -type directory -path $args
}
function prompt
{
"PS " + $(get-location) + "> "
}
& {
for ($i = 0; $i -lt 26; $i++)
{
$funcname = ([System.Char]($i+65)) + ':'
$str = "function global:$funcname { set-location $funcname } "
invoke-expression $str
}
}
Also take into account the following problem. You may have the following error while executing the file located in $profile:
"Microsoft.PowerShell_profile.ps" cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.
Solution:
Check the current execution-policy
PS C:\Windows\System32> Get-ExecutionPolicy
Restricted
PS C:\Windows\System32>
To change the execution policy to allow PowerShell to execute scripts from local files, run the following command:
PS C:\Windows\System32> Set-Executionpolicy RemoteSigned -Scope CurrentUser

Replace MKS with Powershell

can anybody please translate these two scripts from MKS into Powershell? i would like to remove MKS from our ETL tools and accomplish this with Powershell but do not have the chops
1)
FileSize=ls -l $1 | awk '{print $5}'
if [ $FileSize -ge 100000000 ]; then
split -b 60000000 $1 $1
fi
2)
find $1 -type f -name *.txt -mtime +30 -exec rm {} \;
thanks very much
drew
Avoiding using even standard aliases here (eg. can use dir or ls rather than Get-ChildItem):
1) FileSize=ls -l $1 | awk '{print $5}'
$filesize = (Get-ChildItem $name).Length
if [ $FileSize -ge 100000000 ]; then split -b 60000000 $1 $1 fi
if ($filesize -ge 100000000) { ... }
(can't recall function of split)
2) find $1 -type f -name *.txt -mtime +30 -exec rm {} \;
$t = [datetime]::Now.AddSeconds(-30)
Get-ChildItem -path . -recurse -filter *.txt |
Where-Object { $_.CreationTime -gt $t -and $_.PSIsContainer } |
Remove-Item
(Add a -whatif to the Remove-Item to list what would be deleted without deleting them.)
1) Get the size of the file named by $1. If the size is more than 100 megabytes, split it into parts of 60 megabytes each.
MKS
FileSize=`ls -l $1 | awk '{print $5}'`
if [ $FileSize -ge 100000000 ]; then
split -b 60000000 $1 $1
fi
PowerShell
function split( [string]$path, [int]$byteCount ) {
# Find how many splits will be made.
$file = Get-ChildItem $path
[int]$splitCount = [Math]::Ceiling( $file.Length / $byteCount )
$numberFormat = '0' * "$splitCount".Length
$nameFormat = $file.BaseName + "{0:$numberFormat}" + $file.Extension
$pathFormat = Join-Path $file.DirectoryName $nameFormat
# Read the file in $byteCount chunks, sending each chunk to a numbered split file.
Get-Content $file.FullName -Encoding Byte -ReadCount $byteCount |
Foreach-Object { $i = 1 } {
$splitPath = $pathFormat -f $i
Set-Content $splitPath $_ -Encoding Byte
++$i
}
}
$FileSize = (Get-ChildItem $name).Length
if( $FileSize -gt 100MB ) {
split -b 60MB $name
}
Notes: Only the split functionality needed by the question was implemented, and tested just on small file sizes. You may want to look into StreamReader and StreamWriter to perform more efficient buffered IO.
2) In the directory named by $1, find all regular files with a .txt extension that were modified over thirty days ago, and remove them.
MKS
find $1 -type f -name *.txt -mtime +30 -exec rm {} \;
PowerShell
$modifiedTime = (Get-Date).AddDays( -30 )
Get-ChildItem $name -Filter *.txt -Recurse |
Where-Object { $_.LastWriteTime -lt $modifiedTime } |
Remove-Item -WhatIf
Notes: Take off the -WhatIf switch to actually perform the remove operation, rather than previewing it.