使用多变量模式调用 grep
Calling grep with mulitple variables pattern
如何在 tcl 中使用 grep 的多个模式?
set finish [exec bash -c "cat /home/new.txt |grep \"$day\|$yesterday\" > /home/new_tmp.txt"]
它在 bash
控制台中工作
day=26.01.2020
yesterday=26.01.2020
cat /home/new.txt |grep "$day\|$yesterday"
但是对于 Tcl 脚本,文件是空的。
第一次试用将是:
set from /home/new.txt
set to /home/new_tmp.txt
set day 26.01.2020
set yesterday 26.01.2020
# Did you mean these two to be the same?
catch {exec grep "$day|$yesterday" <$from >$to}
# Because grep returns non-zero if it doesn't find anything, which exec converts to an error
您实际上并不经常需要外部 cat
,无论是在 Tcl 中还是在 Bash 中。
注意 grep
匹配 .
与 任何 字符。
一般 grep
提示:由于您正在搜索多个固定字符串,而不是真正的正则表达式,您可以告诉 grep
获得更有效的方法(并解决问题.
被视为元字符而不是完全匹配):
grep -F -e "$day" -e "$yesterday" /home/new.txt > /home/new_tmp.txt
或者只是在纯 tcl 中完成这一切而不是让 shell 参与进来?
set infile [open /home/new.txt r]
set outfile [open /home/new_tmp.txt w]
while {[gets $infile line] >= 0} {
if {[string first $day $line] >= 0 || [string first $yesterday $line] >= 0} {
puts $outfile $line
}
}
close $infile
close $outfile
如何在 tcl 中使用 grep 的多个模式?
set finish [exec bash -c "cat /home/new.txt |grep \"$day\|$yesterday\" > /home/new_tmp.txt"]
它在 bash
控制台中工作
day=26.01.2020
yesterday=26.01.2020
cat /home/new.txt |grep "$day\|$yesterday"
但是对于 Tcl 脚本,文件是空的。
第一次试用将是:
set from /home/new.txt
set to /home/new_tmp.txt
set day 26.01.2020
set yesterday 26.01.2020
# Did you mean these two to be the same?
catch {exec grep "$day|$yesterday" <$from >$to}
# Because grep returns non-zero if it doesn't find anything, which exec converts to an error
您实际上并不经常需要外部 cat
,无论是在 Tcl 中还是在 Bash 中。
注意 grep
匹配 .
与 任何 字符。
一般 grep
提示:由于您正在搜索多个固定字符串,而不是真正的正则表达式,您可以告诉 grep
获得更有效的方法(并解决问题.
被视为元字符而不是完全匹配):
grep -F -e "$day" -e "$yesterday" /home/new.txt > /home/new_tmp.txt
或者只是在纯 tcl 中完成这一切而不是让 shell 参与进来?
set infile [open /home/new.txt r]
set outfile [open /home/new_tmp.txt w]
while {[gets $infile line] >= 0} {
if {[string first $day $line] >= 0 || [string first $yesterday $line] >= 0} {
puts $outfile $line
}
}
close $infile
close $outfile