这就是我试图打破我的头脑......但它仍然不起作用
这是有效的:
awk '{
if ( $2 ~ /FULL/ )
print "TRUE" $0
}' FILE1
但是现在我希望“/ FULL /”部分是动态的,所以我可以通过将它链接到shell变量来改变它。
所以这就是我想要的:
shellfull="/FULL/"
awk -v awkfull=$shellfull '{
if ( $2 ~ awkfull )
print "TRUE" $0
}' FILE1
FILE1的内容为:
84738273 RTF-Ref-FULL-Monday
38473847 The-Wed-DLL-DIFF-Fri-FULL
84839489 FULL
83945940 Schedule_FULL
84928049 Schedule_DIFF
有点看起来它不允许我在“IF”声明中使用awkfull,因为我可以打印awkfull并且它确实显示了shell变量内容。 我的意思是:
print awkfull //this works)
if ( $1 ~ awkfull) //this does NOT work... why?
更新1
所有的回应,一些给了非常好的主意。但我觉得我需要描述我的确切情况。因为我真的需要“/”才能在字符串中的reandom位置找到一个单词,而且我不知道字符串会提前看起来像什么。
所以我想将它用于:
//Set Variable Content:
FULL="/FULL/||/Full/||/full/||/SQL-Backup_Fri-Last-Of-Month/"
DIFF="/DIFF/||/Diff/||/diff/||/San-Element/"
现在我有一个大的日志文件,它在其中一个字符串中有一些字,我将用它来确定它是否算作FULL或DIFF,或者两者都基于$中是否有单词FULL或$ DIFF匹配该字符串中的reandom位置
因此,如果文件中的蜇声如下所示:
84738273 RTF-Ref-full-Monday
38473847 The-Wed-DLL-DIFF-Fri-FULL
84839489 FULL
83945940 Schedule_Full_backup
84928049 Schedule_DIFF
83940392 2_SQL-Backup_Fri-Last-Of-Month-23049
84828348 Schedule_new-build
我希望它成为:
FULL 84738273 RTF-Ref-full-Monday
DIFFFULL 38473847 The-Wed-DLL-DIFF-Fri-FULL
FULL 84839489 FULL
FULL 83945940 Schedule_Full
DIFF 84928049 Schedule_DIFF
FULL 83940392 2_SQL-Backup_Fri-Last-Of-Month-23049
UNKNOWN 84828348 Schedule_new-build
现在重要的是让列表包含要以动态方式进行设置搜索的单词。因此,通过更改变量$ DIFF或$ FULL,它将搜索文件中的不同单词。
所以我想要实现这个目标的方式是:sript概念:
//set filter patterns
FULL="/FULL/||/Full/||/full/||/SQL-Backup_Fri-Last-Of-Month/"
DIFF="/DIFF/||/Diff/||/diff/||/San-Element/"
awk -v full=$FULL -v diff=$DIFF '{ //link the shell variable to awk variable, since a direct shell variable in awk didn't work.
if ( $2 ~ diff ) //find all strings with words defined in $DIFF
print "DIFF" $0 //print "DIFF" in front of the line
if ( $2 ~ full ) //find all strings with words defined in $FULL
print "FULL" $0 //print "FULL" in front of the line
if ( $2 !~ full||diff) //if a line does neighter contain words of $DIFF or $FULL.
print "UNKNOWN" $0 //print "UNKNOWN" in front of the line
}' FILE1 //load the file that needs to be filterd
如果找到尚未定义的计划和策略,则此脚本需要向我及其自身明确说明。这样scipt就知道何时创建了一个新的未知计划或策略,并且可以警告我需要调整其过滤器。它使脚本能够计算计算出的Kbytes到“未知的时间表和策略池”的数量
希望这会让事情变得更加清晰。我需要在字符串中的reandom位置搜索多个reandom字 [FULL,full,SQL-DB,ect。] [这就是为什么我要使用/ $ full / || / $ diff /,或类似的东西]。
答案 0 :(得分:3)
您的字符串不需要/
进行比较。以下是有效的:
shellfull="FULL"
$ shellfull="EMPTY"
$ awk -v patt=$shellfull '$1 ~ patt {print "TRUE" $0}' a
TRUEEMPTY 3928304
$ shellfull="FULL"
$ awk -v patt=$shellfull '$1 ~ patt {print "TRUE" $0}' a
TRUEFULL 2930429
TRUEFULL 3940229
根据您刚发布的输入文件:
$ shellfull="FULL"
$ awk -v patt=$shellfull '$2 ~ patt {print "TRUE " $0}' a
TRUE 84738273 RTF-Ref-FULL-Monday
TRUE 38473847 The-Wed-DLL-DIFF-Fri-FULL
TRUE 84839489 FULL
TRUE 83945940 Schedule_FULL
如果您想要完全匹配:
$ awk -v patt=$shellfull '$2==patt {print "TRUE " $0}' a
TRUE 84839489 FULL
如果你想匹配你通过字符串传递的不同单词,让我们这样做:
awk -v patt=$shellfull 'BEGIN{split(patt,a,"|")}
{for (i in a) if ($2==a[i]) {print "TRUE " $0; next}}' a
获取字符串,用分隔符|
将其展开(您可以定义另一个),然后查找每个给定字符串的完全匹配。
$ shellfull="Schedule_DIFF|FULL"
$ awk -v patt=$shellfull 'BEGIN{split(patt,a,"|")} {for (i in a) if ($2==a[i]) {print "TRUE " $0; next}}' a
TRUE 84839489 FULL
TRUE 84928049 Schedule_DIFF
$ shellfull="FULL"
$ awk -v patt=$shellfull 'BEGIN{split(patt,a,"|")} {for (i in a) if ($2==a[i]) {print "TRUE " $0; next}}' a
TRUE 84839489 FULL
答案 1 :(得分:1)
您可以通过以下方式完成:
shellfull="/FULL/"
awk '{
if ( $1 ~ '$shellfull' )
print "TRUE" $0
}' FILE1
这看起来好像现在正在引用$shellfull
变量,但它实际上是“取消引用”它,以便shell会查看它。
答案 2 :(得分:1)
根据您的最新更新,听起来您需要的只是:
$ FULL="FULL|Full|full|SQL-Backup_Fri-Last-Of-Month"
$ DIFF="DIFF|Diff|diff|San-Element"
$ cat file
84738273 RTF-Ref-full-Monday
38473847 The-Wed-DLL-DIFF-Fri-FULL
84839489 FULL
83945940 Schedule_Full_backup
84928049 Schedule_DIFF
83940392 2_SQL-Backup_Fri-Last-Of-Month-23049
84828348 Schedule_new-build
$ awk -v full="$FULL" -v diff="$DIFF" '{
if ( ( $2 ~ diff ) && ( $2 ~ full ) )
print "DIFFFULL", $0
else if ( $2 ~ diff )
print "DIFF", $0
else if ( $2 ~ full )
print "FULL", $0
else
print "UNKNOWN", $0
}' file
FULL 84738273 RTF-Ref-full-Monday
DIFFFULL 38473847 The-Wed-DLL-DIFF-Fri-FULL
FULL 84839489 FULL
FULL 83945940 Schedule_Full_backup
DIFF 84928049 Schedule_DIFF
FULL 83940392 2_SQL-Backup_Fri-Last-Of-Month-23049
UNKNOWN 84828348 Schedule_new-build
但你需要确定“不同”之类的词语是否可以与“差异”匹配,如果它们恰好出现。
答案 3 :(得分:0)
在命令下运行时出错:
Spacedata_1
是一个文件
mountpoint
是外部变量,具有一些值
grep "$mountpoint" Spacedata_1 |awk -v mountpoint=$mountpoint 'if ($5==mountpoint ) { print $4 }'