我写了一个shell函数,如下所示
extract () {
awk -vbegin="${1}" '/begin/ {flag=1;next} /=====/{flag=0} flag {
print
}
' "$2" | egrep "$3" | egrep -v "$4"
}
然而,当我打电话
extract "Begin Criteria" "First" "Second" "Third1|Third2"
第一个变量“Begin Criteria”似乎无法在/ begin /
中解析我怎样才能使我的通话类似?
awk '/Begin Criteria/ {flag=1;next} /=====/{flag=0} flag {
print
}
' First | egrep 'Second' | egrep -v 'Third1|Third2'
Raw_data_file
First Bundle ====
First 1
Second 1
Third_1 1
Third_2 1
=====
Second Bundle ====
Second 1
Third_1 1
Third_2 1
Third_3 1
=====
Third Bundle ====
Third_1 1
Third_2 1
Third_3 1
=====
First Bundle ====
First 2
Second 2
Third_1 2
Third_2 2
=====
Second Bundle ====
Second 2
Third_1 2
Third_2 2
Third_3 2
=====
Third Bundle ====
Third_1 2
Third_2 2
Third_3 2
=====
用法
extract "Second Bundle" Raw_data_file "Second" "Third1|Third2" Second.out
extract "Third Bundle" Raw_data_file "Third1|Third2" "" Third.out
extract "First Bundle" Raw_data_file "" "" First.out
预期
First.out
First 1
Second 1
Third_1 1
Third_2 1
First 2
Second 2
Third_1 2
Third_2 2
Second.out
Second 1
Second 2
Third.out
Third_1 1
Third_2 1
Third_1 2
Third_2 2
答案 0 :(得分:1)
要在awk中使用变量,您可以执行以下操作:
form.getvalue('toggle-event')
当您使用awk -vbegin="${1}" '$0 ~ begin {flag=1;next} /=====/{flag=0} flag {
print
}'
时,您确实将文字/begin/
与变量begin
完全匹配。
答案 1 :(得分:0)
当你使用awk时,你永远不需要使用管道来/从grep等,因为awk可以做任何有用的事情,grep可以做。将您的功能重写为:
extract () {
awk -v begin="$1" -v plus="$3" -v minus="$4" '
/=====/ {flag=0}
flag && ($0 ~ plus) && ($0 !~ minus)
$0 ~ begin {flag=1}
' "$2"
}
未经测试,因为您没有提供任何样本输入或预期输出。