从Java运行Echo(running Echo from Java)
我正在尝试Runtime.exec()方法来运行命令行进程。
我编写了这个示例代码,它运行时没有问题,但不会在c:\ tmp.txt中生成文件。
String cmdLine = "echo foo > c:\\tmp.txt"; Runtime rt = Runtime.getRuntime(); Process pr = rt.exec(cmdLine); BufferedReader input = new BufferedReader( new InputStreamReader(pr.getInputStream())); String line; StringBuilder output = new StringBuilder(); while ((line = input.readLine()) != null) { output.append(line); } int exitVal = pr.waitFor(); logger.info(String.format("Ran command '%s', got exit code %d, output:\n%s", cmdLine, exitVal, output));
输出是
INFO 21-04 20:02:03,024 - Ran命令'echo foo> c:\ tmp.txt',退出代码0,输出:foo> c:\ tmp.txt
I'm trying out the Runtime.exec() method to run a command line process.
I wrote this sample code, which runs without problems but doesn't produce a file at c:\tmp.txt.
String cmdLine = "echo foo > c:\\tmp.txt"; Runtime rt = Runtime.getRuntime(); Process pr = rt.exec(cmdLine); BufferedReader input = new BufferedReader( new InputStreamReader(pr.getInputStream())); String line; StringBuilder output = new StringBuilder(); while ((line = input.readLine()) != null) { output.append(line); } int exitVal = pr.waitFor(); logger.info(String.format("Ran command '%s', got exit code %d, output:\n%s", cmdLine, exitVal, output));
The output is
INFO 21-04 20:02:03,024 - Ran command 'echo foo > c:\tmp.txt', got exit code 0, output: foo > c:\tmp.txt
原文:https://stackoverflow.com/questions/2684985
最满意答案
您可以使用单词边界
\\b
来区分它们。\\bno\\b
将不仅匹配no
前面和后面的单词字符:grepl("\\bno\\b", x1) # [1] FALSE grepl("\\bno\\b", x2) # [1] TRUE
You can use word boundary
\\b
to distinguish them.\\bno\\b
will matchno
only without preceding and following word characters:grepl("\\bno\\b", x1) # [1] FALSE grepl("\\bno\\b", x2) # [1] TRUE
相关问答
更多-
r - grepl vs比赛(r - grepl vs matches)[2023-12-03]
这是因为你没有设置ignore.case = F > dt %>% select(matches('^enj', ignore.case = F) ) enj1 enj2 bbc 1 1 bca 1 1 > It's because you didn't set ignore.case = F. > dt %>% select(matches('^enj', ignore.case = F) ) enj1 enj2 bbc 1 1 bca 1 ... -
使用grepl ,我们可以尝试使用is.na来获得预期的输出 as.logical(NA^is.na(v1) * grepl("test", v1)) #[1] NA TRUE FALSE NA 或与ifelse ifelse(is.na(v1), NA, grepl("test", v1)) #[1] NA TRUE FALSE NA ifelse(is.na(v2), NA, grepl("test", v2)) #[1] NA TRUE FALSE NA ...
-
这样做的诀窍: vec = addr$addr testData$addr = apply(testData, 1, function(u){ bool = sapply(vec, function(x) grepl(x, u[['content']])) if(any(bool)) vec[bool] else NA }) This does the trick: vec = addr$addr testData$addr = apply(testData, 1, function( ...
-
R中的AND grepl操作(AND grepl operation in R)[2023-12-18]
您可以在1,5,6左右使用字边界\\b : grepl("\\b[156]\\b", c) # [1] TRUE 如果要检查字符串中是否包含所有1,5,6 ,可以在模式位置上向量化grepl : all(Vectorize(grepl)(paste("\\b", c(1,5,6), "\\b", sep = ""), c)) # [1] TRUE 当我们从字符串中删除1 : c <- "NA NA 4 5 6 NA NA 9 10 NA NA 13 14 15 16 17 18 19 20 NA NA ... -
您可以使用单词边界\\b来区分它们。 \\bno\\b将不仅匹配no前面和后面的单词字符: grepl("\\bno\\b", x1) # [1] FALSE grepl("\\bno\\b", x2) # [1] TRUE You can use word boundary \\b to distinguish them. \\bno\\b will match no only without preceding and following word characters: grepl("\\bno\\ ...
-
您可以使用grepl()生成的逻辑向量来索引vec。 txt_paths <- vec[grepl(".txt$", vec)] You can use the logical vector produced by grepl() to index vec. txt_paths <- vec[grepl(".txt$", vec)]
-
for (target in c('testing','evaluating','analyzing') ) { test[grepl(target, test$url),'subdir' ] <- target } test url content subdir 1 google.com/testing/duck 1 testing 2 google.com/evaluating/d ...
-
你只需要正确地逃避正则表达式中的斜线 ff<-function(x) grepl('\\bx\\b',x, perl=T) ff(c("axa","a x a", "xa", "ax","x")) # [1] FALSE TRUE FALSE FALSE TRUE You just need to properly escape the slash in your regex ff<-function(x) grepl('\\bx\\b',x, perl=T) ff(c("axa","a x a", ...
-
这样的事情怎么样: df$SHOT_TYPE <- ifelse(grepl('d Jumper|t Jumper', df$DATA_TEXT), 'Jumper', ifelse(grepl('e Jumper', df$DATA_TEXT), 'Three', NA)) 产生输出: SHOT CLASS DATA_TEXT SHOT_TYPE 1 shot1 missed missed Jumper ...
-
使用REGEX过滤gre中的数据帧的grepl()子集()(subset() with grepl() using REGEX for filtering a dataframe in R)[2022-06-27]
有各种各样的问题。 一个问题是模式中的额外空格。 删除它们或使用perl = TRUE的自由间距修改器(?x) 。 无论哪种方式,你必须摆脱字符类中的空格。 [ln]匹配“m”, [l - n]不匹配,即使用(?x) 。 您可以在此处阅读有关自由间距修改器及其在字符类内外的影响的更多信息。 另一个问题是,在你的第一个grepl ,你是在一个长度为10的向量(字符向量?我们无法从示例中看出)中搜索的。第6个位置的TRUE对于5行data.frame是什么意思? 返回5行数据帧的第6行没有意义。 相反,您可以查 ...