Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在shell中为字符串阻塞空间_Shell_Formatting_Printf_Vi - Fatal编程技术网

在shell中为字符串阻塞空间

在shell中为字符串阻塞空间,shell,formatting,printf,vi,Shell,Formatting,Printf,Vi,如何使用printf命令在shell中为字符串阻塞特定空间 例如,结果是 tom@x.com 10 john@x.com 11 andrew@x.com 12 thomas_sean@x.com 15 当我在编码中使用的命令是 printf $user$i $time 期望的结果是 tom@x.com 10 john@x.com 11 andrew@x.com 12 thomas_sean@x.com 15 我的代码如下- echo

如何使用
printf
命令在shell中为字符串阻塞特定空间 例如,结果是

tom@x.com 10
john@x.com 11
andrew@x.com 12
thomas_sean@x.com 15
当我在编码中使用的命令是

printf $user$i $time
期望的结果是

tom@x.com          10
john@x.com         11
andrew@x.com       12
thomas_sean@x.com  15
我的代码如下-

    echo $h | cut -f$a -d" " 

`printf "\t${t[$a]}\t\t $hour:$min:$sec\n"` 

您可以尝试以下方法:

printf("%-10s%-50s", $1, $2)
我没有测试它,但我认为它会起作用。 如果没有,看看这个,有人解决了同样的问题:)


希望有帮助

将输出导入
列-t

$ column -t << END
tom@x.com 10
john@x.com 11
andrew@x.com 12
thomas_sean@x.com 15
END
可能的脚本
如果需要,可以稍微调整
%-20s
%-17s
%-18s
),但基本思想是保留适当数量的空格,左对齐字符串,然后是空白,然后是“时间”。换行符的
\n
是必需的
printf
不会添加换行符,除非您要求它这样做。

显示更多代码。解决方案可能取决于您的操作方式。您的代码相当混乱。
printf$user$i$time
部分很奇怪-格式字符串发生了什么变化,以及
$i
是什么?
echo$h | cut-f$a-d”“
部分难以理解;什么是
$h
,什么是
$a
printf“\t${t[$a]}\t\t$hour:$min:$sec\n”
片段令人费解;同样,没有可见的格式字符串,不清楚
t
数组中有什么;
$a
中的内容,或
$hour
$min
$sec
中的内容,没有理由像后引号那样执行
printf
命令的输出。请创建一个SSCCE()。@devnull-t[$a]=
echo$h | cut-f$a-d”“
printf“\t${t[$a]}\t\t$hour:$min:$sec\n“printf”\t${t[$a]}\t\t$hour:$min:$sec\n>>final.logt[$a]=
echo$h | cut f$a-d”“
printf>>决赛。log@user2663468更新问题,而不是写评论。谢谢你的建议,是的,这是可行的,但我可以在控制台上完成,而不是在我的代码中。在我的代码中执行这个命令我该怎么做?因为这个printf命令在for循环中,我已经更新了有问题的代码,请检查并提出建议。@user2663468:您必须显示更多的代码,以便任何人都能帮助您。
tom@x.com          10
john@x.com         11
andrew@x.com       12
thomas_sean@x.com  15
while read -r user time
do
    printf "%-20s %s\n" "$user" "$time"
done <<'EOF'
tom@x.com 10
john@x.com 11
andrew@x.com 12
thomas_sean@x.com 15
EOF
tom@x.com            10
john@x.com           11
andrew@x.com         12
thomas_sean@x.com    15