使用 awk,该system命令返回命令输出和退出代码。
但是,输出和退出代码之间有一个新行,这是我真的不想要的。
例子:
BEGIN {
    FS = ","
}
{
    print system("echo toto")
}
输出几行文件:
toto
0
toto
0
toto
0
toto
0
toto
0
toto
0
toto
0
toto
0
toto
0
toto
0
toto
0
在另一篇文章中,我尝试了以下方法来删除新行:
printf "%s,", system("echo toto")
但它有以下结果:
toto
0,toto
0,toto
0,toto
0,toto
0,toto
0,toto
0,toto
0,toto
0,toto
0,toto
0,
我怎样才能:
awksystem()永远不会“返回”任何输出。如果它运行的命令写入输出,则输出会直接进入而不对标准输出进行任何修改 - 在您的情况下,因为您没有将其重定向到您的终端。仅system()返回退出状态,并再次将该状态加上换行符打印到标准输出,因为您没有重定向它。print system()
要捕获命令的输出并在 awk 中对其进行操作,请stringcommand | getline [var]在必要时使用repeated来获取多行,并在必要时使用repeated来close(stringcommand)获取状态。
$ seq 5 | awk '{"echo toto"|getline x; print NR ":x=" x ",status=" close("echo toto") " and Robert is the brother of your parent"}'
1:x=toto,status=0 and Robert is the brother of your parent
2:x=toto,status=0 and Robert is the brother of your parent
3:x=toto,status=0 and Robert is the brother of your parent
4:x=toto,status=0 and Robert is the brother of your parent
5:x=toto,status=0 and Robert is the brother of your parent