1

这就是我试图打破我的头......但它仍然不起作用

这是有效的:

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

Ty 所有的回应,有些给出了非常好的想法。但我觉得我需要描述我的确切情况。因为我真的需要“/”才能在字符串中的任意位置找到单词,而且我事先不知道字符串会是什么样子。

所以这就是我想要使用它的目的:

//Set Variable Content:
FULL="/FULL/||/Full/||/full/||/SQL-Backup_Fri-Last-Of-Month/"
DIFF="/DIFF/||/Diff/||/diff/||/San-Element/"

现在我有一个很大的日志文件,它的一个字符串中有一些单词,我将使用这些单词来确定它是 FULL 还是 DIFF,或者两者都基于 $FULL 或 $ 中是否有单词DIFF 匹配该字符串中的随机位置

因此,如果文件中的 stings 如下所示:

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 就知道何时创建了新的未知计划或策略,并可以警告我需要调整其过滤器。它使脚本能够计算计算出的千字节将进入“未知的计划和策略池”

希望这能让事情更清楚一些。我需要在字符串内的随机位置中搜索多个随机词[FULL,full,SQL-DB,ect.] [这就是为什么我想使用 /$full/||/$diff/ 或类似的东西] .

4

4 回答 4

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 

更新 2

如果您想匹配通过字符串传递的不同单词,让我们这样做:

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
于 2013-10-09T14:58:15.997 回答
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

但是如果碰巧出现,您需要确定“不同”之类的词是否可以与“差异”匹配。

于 2013-10-09T20:37:57.980 回答
1

你可以简单地做到这一点:

shellfull="/FULL/"

awk '{
     if ( $1 ~ '$shellfull' )
     print "TRUE" $0
}' FILE1

这看起来像是现在引用了$shellfull变量,但实际上是“取消引用”它,以便 shell 会查看它。

于 2013-10-09T15:10:48.383 回答
0

运行以下命令时出错:

Spacedata_1是一个文件 mountpoint是具有某些值的外部变量

grep "$mountpoint" Spacedata_1 |awk -v mountpoint=$mountpoint 'if ($5==mountpoint ) { print $4 }'
于 2017-06-29T12:23:37.877 回答