将grep结果字符串分配给Windows批处理文件中的变量

时间:2022-03-05 12:05:36

In a batch file, I can send the results from grep to a file like this:

在批处理文件中,我可以将grep的结果发送到如下文件:

grep -o -P -f search_text.txt text_file.txt >outputfile.txt

But I don't want the result to go to a file. I want the result to be stored in a variable that I can use later in the batch file. I want to do this:

但我不希望结果转到文件。我希望将结果存储在我稍后可以在批处理文件中使用的变量中。我想做这个:

grep -o -P -f search_text.txt text_file.txt >my_variable

But of course that doesn't work. How can I get grep results assigned to a variable?

但当然这不起作用。如何将grep结果分配给变量?

2 个解决方案

#1


grep -o -P -f search_text.txt text_file.txt >outputfile.txt

grep -o -P -f search_text.txt text_file.txt> outputfile.txt

then do

var=$(head -n 1 outputfile.txt) (read first line)

var = $(head -n 1 outputfile.txt)(读取第一行)

and you have the result in var

你得到的结果是var

#2


tmpStr=grep -o -P -f search_text.txt text_file.txt

tmpStr = grep -o -P -f search_text.txt text_file.txt

if it won't work, try :

如果它不起作用,请尝试:

tmpStr=$(grep -o -P -f search_text.txt text_file.txt)

tmpStr = $(grep -o -P -f search_text.txt text_file.txt)

#1


grep -o -P -f search_text.txt text_file.txt >outputfile.txt

grep -o -P -f search_text.txt text_file.txt> outputfile.txt

then do

var=$(head -n 1 outputfile.txt) (read first line)

var = $(head -n 1 outputfile.txt)(读取第一行)

and you have the result in var

你得到的结果是var

#2


tmpStr=grep -o -P -f search_text.txt text_file.txt

tmpStr = grep -o -P -f search_text.txt text_file.txt

if it won't work, try :

如果它不起作用,请尝试:

tmpStr=$(grep -o -P -f search_text.txt text_file.txt)

tmpStr = $(grep -o -P -f search_text.txt text_file.txt)