[英]redirecting awk output to file in bash
I am working on a bash script that captures beacon frame packets (without bad fcs) and output them in a preferred format, but I am having problem redirecting the outptut to a file. 我正在处理一个bash脚本,该脚本捕获信标帧数据包(无错误的fcs)并以首选格式输出它们,但是我在将输出重定向到文件时遇到问题。
This is my command line when I am redirecting to a file called temp
当我重定向到名为temp
的文件时,这是我的命令行
tcpdump -I -i mon0 -vv 2>/dev/null|awk -F ',| ' 'BEGIN{printf "%-10s %-25s%-10s\n","OPTION NO.","ESSID(Beacon Frames)","CHANNEL NO."};$0~/Beacon/{for(i=1;i<=NF;++i){if(($i~/^\([^^]+\)$/) && !($i in arr) && ($0~/CH:/) && !($0~/tsft bad-fcs/)){NR=++c;arr[$i]=1;gsub(/\(|\)/,"",$i);printf("%-10s %-25s",NR,$i);for(x=1;x<=NF;++x){if($x~/^CH:/){print $x " "$(x+1) "\tHit Ctrl+C to stop scan"}}}}}' >> temp
The command line above works fine in a terminal when I am not redirecting to a file (the output is shown). 当我不重定向到文件(显示输出)时,上面的命令行在终端中运行良好。 When I am redirecting to a file, I am seeing the file exist with no output. 当我重定向到文件时,我看到文件存在而没有输出。
I tried the following 我尝试了以下
1.Pipe the command line output like tee -a temp
(to output to stdout and file) 1,将命令行输出像tee -a temp
一样输入管道(输出到stdout和文件)
example 例
tcpdump -I -i mon0 -vv 2>/dev/null|awk -F ',| ' 'BEGIN{printf "%-10s %-25s%-10s\n","OPTION NO.","ESSID(Beacon Frames)","CHANNEL NO."};$0~/Beacon/{for(i=1;i<=NF;++i){if(($i~/^\([^^]+\)$/) && !($i in arr) && ($0~/CH:/) && !($0~/tsft bad-fcs/)){NR=++c;arr[$i]=1;gsub(/\(|\)/,"",$i);printf("%-10s %-25s",NR,$i);for(x=1;x<=NF;++x){if($x~/^CH:/){print $x " "$(x+1) "\tHit Ctrl+C to stop scan"}}}}}'|tee -a temp
I tried 我试过了
exec > temp command line above
Can this be a buffering issue since packet capturing is rapid? 由于数据包捕获速度很快,这可能是一个缓冲问题吗?
How can the results of the above command line be redirected to a file? 如何将以上命令行的结果重定向到文件?
Note: mon0
in the command line represents the monitor interface I started on my wireless adapter using airmon-ng
注意:命令行中的mon0
代表我使用airmon-ng
在无线适配器上启动的监视器界面
edit: the breakdown of the codes are as follows 编辑:代码明细如下
BEGIN {
FS=",| "
printf "%-10s %-25s%-10s\n","OPTION NO.","ESSID(Beacon Frames)","CHANNEL NO."
}
$0~/Beacon/ {
for(i=1;i<=NF;++i) {
if(($i~/^\([^^]+\)$/) && !($i in arr) && ($0~/CH:/) && !($0~/tsft bad-fcs/)) {
NR=++c
arr[$i]=1
gsub(/\(|\)/,"",$i)
printf("%-10s %-25s",NR,$i)
for(x=1;x<=NF;++x) {
if($x~/^CH:/) {
print $x " "$(x+1) "\tHit Ctrl+C to stop scan"
}
}
}
}
}
As i mentioned the codes work fine..it is just the redirection issue..do offer improvements to the code if needed. 正如我提到的那样,代码可以正常工作。它只是重定向问题。如果需要,可以对代码进行改进。
May not be the answer and i haven't worked with bash in forever but possibly try sending it to a text file. 可能不是答案,我还没有永远使用bash,但可能尝试将其发送到文本文件。 temp may be treated as a directory. temp可以被视为目录。 Instead try exec > temp.txt 而是尝试执行> temp.txt
After a bit of reading on awk and gawk..I came across a very interesting topic on buffering behaviour in one of my text.This solved my problem..I changed the buffering behaviour of awk using the following 在对awk和gawk进行了一些阅读之后..我在我的一篇文章中遇到了一个关于缓冲行为的非常有趣的话题。这解决了我的问题..
fflush("") ==> gawk and newer versions of awk
or 要么
system("")==> older versions of awk
This forces awk to flush its output immediately for every input line. 这将迫使awk立即为每条输入线刷新其输出。
I tried each of the aforementioned functions in my command line and my output was immediately redirected to my file temp
. 我在命令行中尝试了上述每个功能,我的输出立即被重定向到了文件temp
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.