我想从 shell 脚本将两个变量传递到 AWK 脚本。
#!/bin/bash
HeaderSize=$(grep -n "# end header data" input.list | tr ":" "\n" | head -n 1)
RecordSize=$(grep -n "# Number of unique" input.list | tr ":" "\n" | tail -n 1 | sed 's/,//g')
echo $HeaderSize
echo $RecordSize
head -n $HeaderSize input.list > temp.list
./parse.awk -v headersize=$HeaderSize -v recordsize=$RecordSize < input.list >> temp.list
文件解析.awk看起来像这样:
#!/bin/gawk -f
BEGIN {
curline=1;
excludeline=0;
newrecordsize=0;
}
{
if (curline < $headersize) {
curline++;
} else {
if (($2 !~ /test1/) && ($2 !~ /test2/) && ($2 !~ /test3/)) {
print $0;
} else {
excludeline++;
}
}
}
END {
print "#", excludeline, "lines excluded";
newrecordsize = recordsize - excludeline;
printf "# Number of unique items after exclusions: %'d\n", newrecordsize;
}
所以我只想直接复制标题,然后排除一些数据。
当我运行 Bash 文件时,gawk 执行时出现错误:
38
94108
gawk: ./parse.awk:6: fatal: cannot open file `94108' for reading (No such file or directory)
94108 是 RecordSize 的值或我尝试传递的第二个变量。
我该如何修复这个错误?
编辑:下面找到的答案与变量上的双引号有关,显然我应该在任何地方都这样做。这是我当前的脚本,运行良好。
#!/bin/bash
HeaderSize=$(grep -n "# end header data" input.list | tr ":" "\n" | head -n 1)
RecordSize=$(grep -n "# Number of unique" input.list | tr ":" "\n" | tail -n 1 | sed 's/,//g')
echo "$HeaderSize"
echo "$RecordSize"
head -n "$HeaderSize" input.list > temp.list
./parse.awk -v "headersize=$HeaderSize" -v "recordsize=$RecordSize" < input.list >> temp.list
我还清理了 awk 脚本
#!/bin/gawk -f
BEGIN {
excludeline=0;
newrecordsize=0;
}
NR > headersize {
if (($2 !~ /test1/) && ($2 !~ /test2/) && ($2 !~ /test3/)) {
print $0;
} else {
excludeline++;
}
}
END {
print "#", excludeline, "lines excluded";
newrecordsize = recordsize - excludeline;
printf "# Number of unique domains after exclusions: %'d\n", newrecordsize;
}
答案1
$RecordSize
我怀疑;的开头有空格引用你的变量:
echo "$HeaderSize"
echo "$RecordSize"
./parse.awk -v "headersize=$HeaderSize" -v "recordsize=$RecordSize" < input.list >> temp.list