unix - sed replace file of file with error undefined label - unix

I'd like to replace the entire line
TEMPLATE_FILE = file.tpl with
TEMPLATE_FILE = tester.sh
I have a file called file.config
test0
TEMPLATE_FILE = file.tpl
test1
test2
OUTPUT_DIR = out
I would like the replace
TEMPLATE_FILE = file.tpl to TEMPLATE_FILE = tester.sh
OUTPUT_DIR = out to OUTPUT_DIR = in
sed -i 's/(OUTPUT_DIR).*/(OUTPUT_DIR = in)/g' file.config
I get this error
sed: 1: "file.config": undefined label 'pc.config'
The end result would be
test0
TEMPLATE_FILE = tester.sh
test1
test2
OUTPUT_DIR = in
Any help would be greatly appreciated.

sed -i -E "s/(TEMPLATE_FILE = file.tpl)/TEMPLATE_FILE = tester.sh/" bpc2.config

Judging from the error message, you may using MacOS. Then the -i option requires a string as an argument. Would you please try:
sed -E -i '' 's/(TEMPLATE_FILE).*/\1 = tester.sh/g; s/(OUTPUT_DIR).*/\1 = in/g' file.config

Related

Add stdout = TRUE to R's system2 command and receive warning. Why?

I require the number of Firefox processes running on Linux (Ubuntu) be stored in a variable in an R script. By itself the system2 command I use seems to work. However, when I add stdout = TRUE to capture the info in a character vector I get a warning. Why the warning?
system2(command = "ps", args = "aux | grep [f]irefox -c")
# 0
system2(command = "ps", args = "aux | grep [f]irefox -c", stdout = TRUE)
Warning message:
In system2(command = "ps", args = "aux | grep [f]irefox -c", stdout = TRUE) :
running command ''ps' aux | grep [f]irefox -c' had status 1
Use ef instead of aux as the argument to ps. aux for BSD and ef and variants for standard syntex, as per man ps.
system2('ps', '-ef | grep [f]irefox -c', stdout = TRUE)
[1] "12"

Execute multiline R expression from shell (with indentation)

I'm trying to achieve what you might naively write as:
R -e "
rmarkdown::render(
'MyDocument.Rmd',
params = list(
year = 2017
),
output_file = 'ExampleRnotebook.html'
)
"
So that I can make nicely formatted submission scripts to run on a cluster.
I've tried some variants on the below, I'm wondering if there might be an alternative approach to do this with the R -f flag?
read -r -d '' EXP << EOF
rmarkdown::render(
'MyDocument.Rmd',
params = list(
year = 2017
),
output_file = 'ExampleRnotebook.html'
)
EOF
R -e "$EXP"
but I get a series of errors that look like this:
ARGUMENT 'params~+~=~+~list(' __ignored__
for the different lines of the expression, followed by:
> rmarkdown::render(
+
+ Error: unexpected end of input
To reproduce:
MyDocument.Rmd =
---
title: "R Notebook"
output: html_notebook
params:
year: 0000
---
```{r}
params$year
```
This works fine:
read -r -d '' EXP <<- EOF
rmarkdown::render('MyDocument.Rmd', params = list(year = 2017 ), output_file = 'ExampleRnotebook.html')
EOF
R -e "$EXP"
but gets hard to read with longer param lists
This works for me (R version 3.5.0):
R --no-save <<code
for(i in 1:3) {
i +
2
}
print(i)
runif(5,
1,10)
code
Note: line-breaks and paddings are intentional.

Unexpected '/' in environment variables

Every time I restart R, I get a message in the console:
Error: 2:24: unexpected '/'
2: Sys.setenv(BINPREF = C:/
I've tried unsetting BINREF (Sys.unset...) or setting to an empty string, or adding a double backslash when setting (\\) but the error persists.
This is what it is currently set to: C:\Rtools\mingw_$(WIN)\bin\
It was set with:
cat('Sys.setenv(BINPREF = "C:/Rtools/mingw_$(WIN)/bin/")',
file = file.path(Sys.getenv("HOME"), ".Rprofile"),
sep = "\n", append = TRUE)
What can I do? Is there anyway I can delete BINPREF?
Fixed with
cat('Sys.setenv(BINPREF = "C:/Rtools/mingw_$(WIN)/bin/")',
file = file.path(Sys.getenv("HOME"), ".Rprofile"),
sep = "\n", append = FALSE)
Creates new .Rprofile

How to run a VBS script from R, while passing arguments from R to VBS

Let's say I want to run a VBS script from R, and I want to pass a value from R to that script.
For example, in a simple file called 'Msg_Script.vbs', I have the code:
Dim Msg_Text
Msg_Text = "[Insert Text Here]"
MsgBox("Hello " & Msg_Text)
How do I run this script using R, while editing the parameters and/or variables in R? In the above script for instance, how would I edit the value of the Msg_Text variable?
Another way would be to pass the value as an argument to the VBScript
You'd write the VBS as follows:
Dim Msg_Text
Msg_Text = WScript.Arguments(0)
MsgBox("Hello " & Msg_Text)
And then you'd create a system command in R like this:
system_command <- paste("WScript",
'"Msg_Script.vbs"',
'"World"',
sep = " ")
system(command = system_command,
wait = TRUE)
This approach matches the arguments by position.
If you wanted, you could use named arguments instead. This way, your VBS would look like this:
Dim Msg_Text
Msg_Text = WScript.Arguments.Named.Item("Msg_Text")
MsgBox("Hello " & Msg_Text)
And then you'd create a system command in R like this:
system_command <- paste("WScript",
'"Msg_Script.vbs"',
'/Msg_Text:"World"',
sep = " ")
system(command = system_command,
wait = TRUE)
Here's a somewhat-hackish solution:
Read the lines from the vbs script into R (using readLines()):
vbs_lines <- readLines(con = "Msg_Script.vbs")
Edit the lines in R by finding and replacing specific text:
updated_vbs_lines <- gsub(x = vbs_lines,
pattern = "[Insert Text Here]",
replacement = "World",
fixed = TRUE)
Create a new VBS script using the updated lines:
writeLines(text = updated_vbs_lines,
con = "Temporary VBS Script.vbs")
Run the script using a system command:
full_temp_script_path <- normalizePath("Temporary VBS Script.vbs")
system_command <- paste0("WScript ", '"', full_temp_script_path, '"')
system(command = system_command,
wait = TRUE)
Delete the new script after you've run it:
file.remove("Temporary VBS Script.vbs")

r system doesn't work when trying 7zip

Hi I'm trying to use R to control unzipping a file.
I've added 7z into PATH, and did
7z e hat.2015-09-26T01-10-02.gz
and it worked.
Now in R, i'm in the same directory, and i tried
> command1 = paste0('7z e ', drop.file)
> command1
[1] "7z e hat.2015-09-26T01-10-02.gz"
> system(command1, intern=T)
Error in system(command1, intern = T) : '7z' not found
> system2(command1)
Warning message:
running command '"7z e hat.2015-09-26T01-10-02.gz"' had status 127
> shell(command1)
'7z' is not recognized as an internal or external command,
operable program or batch file.
Warning messages:
1: running command 'C:\Windows\system32\cmd.exe /c 7z e hat.2015-09-26T01-10-02.gz' had status 1
2: In shell(command1) :
'7z e hat.2015-09-26T01-10-02.gz' execution failed with error code 1
It's little bit raw, but try this out (:
Only Windows. It's uses CMD
ZiparEm7zip = function(sQualPasta)
{
sWDTava = getwd()
setwd(dirname(sQualPasta))
sQuem = gsub("[/]", "\\\\", sQualPasta)
dirname(sQuem)
NomeArquivo = paste0("eufaco7zip",".bat")
sNomePasta7zip = basename(sQuem)
sArquivoSaida = basename(sQuem)
sQualPasta7zip = sQuem
if(dir.exists("C:/Program Files/7-Zip/"))
{
sTexto = "set PATH=%PATH%;C:\\Program Files\\7-Zip\\"
}else if(dir.exists("C:/Program Files (x86)/7-Zip/"))
{
sTexto = "set PATH=%PATH%;C:\\Program Files (x86)\\7-Zip\\"
}else
{
stop("Error, o 7zip not installed?")
}
sTexto2 = paste0("\npushd ",sQualPasta7zip)
sTexto3 = paste0("\n7z a -r ../",sArquivoSaida," *")
sTexto4 = '\n( del /q /f "%~f0" >nul 2>&1 & exit /b 0 )'
cat(sTexto,file=NomeArquivo,append = TRUE)
cat(sTexto2,file=NomeArquivo,append = TRUE)
cat(sTexto3,file=NomeArquivo,append = TRUE)
cat(sTexto4,file=NomeArquivo,append = TRUE)
shell.exec(NomeArquivo)
while(file.exists(NomeArquivo))
{
Sys.sleep(10)
}
setwd(sWDTava)
return(TRUE)
}
Try like:
ZiparEm7zip("F:/MYFOLDER")

Resources