Bash 将剪切和回显的管道输出到文件中的同一行

Bash 将剪切和回显的管道输出到文件中的同一行,bash,shell,unix,pipe,echo,Bash,Shell,Unix,Pipe,Echo,我有一个bash脚本,它使用grep和cut将数据发送到输出文件中。我没能实现的是将计数器回送到文件中的同一行,作为cut的输出 这就是我尝试过的: { echo -n "B " & input_file.dat | grep -i "Total net con" | cut -d' ' -f10,12; } >> output_file.dat 我在output\u file.dat中得到的内容类似于: B result_from_grep_and_cut_1 resul

我有一个bash脚本,它使用grep和cut将数据发送到输出文件中。我没能实现的是将计数器回送到文件中的同一行,作为cut的输出

这就是我尝试过的:

{ echo -n "B " & input_file.dat | grep -i "Total net con" | cut -d' ' -f10,12; } >> output_file.dat
我在
output\u file.dat
中得到的内容类似于:

B result_from_grep_and_cut_1
result_from_grep_and_cut_2
result_from_grep_and_cut_3
...
如您所见,
B
仅出现在第一行中。我想做的是在每行的开头得到
B
。我该怎么做

谢谢您的帮助。

要在每行开头获得
“B”
,请使用:

some_command | sed 's/^/B /'
例如:

ps | sed 's/^/B /'
使用单个awk表达式:

awk 'BEGIN{ IGNORECASE=1 }/Total net con/{ print "B ",$10,$12 }' input_file.dat > output_file.dat

为什么要执行
echo-n“B”&input_file.dat
?@batMan我以为它会在每次迭代时执行echo命令(我使用
-n
来避免echo后的换行符),但显然我错了。多亏了@anubhava,现在我知道了sed命令
sed