格式化Shell脚本的输出

时间:2017-04-22 21:18:47

标签: bash shell

我正在开发一个shell脚本,它将stdin或file作为输入,并根据参数打印行或列的平均值和中位数。

计算列的平均值时,输出需要打印出以下内容(选项卡):

enter image description here

我的输出目前看起来像这样(没有空格或制表符):

平均值:

92480654263

中位数:

6368974

有没有办法用标签回显平均值和中位数,这样每个平均值和中位数组合左对齐?以下是我打印平均值的示例:

echo "Averages:"
        while read i
        do
            sum=0
            count=0
            mean=0
            #Cycle through the numbers in the rows
            for num in $i
            do
                #Perform calculations necessary to determine the average and median
                sum=$(($sum + $num))
                count=`expr $count + 1`
                mean=`expr $sum / $count`
            done
            echo -n "$mean"
        done < $1

3 个答案:

答案 0 :(得分:1)

man echo

   -e     enable interpretation of backslash escapes

   If -e is in effect, the following sequences are recognized:

   \t     horizontal tab

我试试echo -n -e "$mean\t",虽然没有测试过。

答案 1 :(得分:1)

您应该使用printf。例如,这将打印一个值,后跟一个选项卡

printf "%s\t" "$mean"

如果需要,您可以通过添加参数来实际打印多个由制表符分隔的值:

printf "%s\t" "$mean" "$count"

您可以使用数组扩展来打印由制表符分隔的多个值:

printf "%s\t" "${my_array[@]}"

printf超过echo的优势之一是灵活格式化字符串的可用性,以及printf的实现在shell和操作中的变化小于echo的事实系统

答案 2 :(得分:0)

您可以尝试使用column命令,但它确实需要执行其他步骤:

// Register the serviceWorker script at /serviceworker.js from our server if supported
if (navigator.serviceWorker) {
  navigator.serviceWorker.register('/serviceworker.js')
  .then(function(reg) {
    console.log('Service worker change, registered the service worker');
  });
}
// Otherwise, no push notifications :(
else {
  console.error('Service worker is not supported in this browser');
}

// When serviceWorker is supported, installed, and activated,
// subscribe the pushManager property with the vapidPublicKey
navigator.serviceWorker.ready.then((serviceWorkerRegistration) => {
  serviceWorkerRegistration.pushManager
  .subscribe({
    userVisibleOnly: true,
    applicationServerKey: window.vapidPublicKey
  });
});

$('.webpush-button').on('click', (e) => {
  navigator.serviceWorker.ready
  .then((serviceWorkerRegistration) => {
    serviceWorkerRegistration.pushManager.getSubscription()
    .then((subscription) => {
      $.post('/push', {
        subscription: subscription.toJSON(),
        message: 'You clicked a button!'
      });
    });
  });
});

// Let's check if the browser supports notifications
if (!("Notification" in window)) {
  console.error("This browser does not support desktop notification");
}

// Let's check whether notification permissions have already been granted
else if (Notification.permission === "granted") {
  console.log("Permission to receive notifications has been granted");
}

// Otherwise, we need to ask the user for permission
else if (Notification.permission !== 'denied') {
  Notification.requestPermission(function (permission) {
  // If the user accepts, let's create a notification
    if (permission === "granted") {
      console.log("Permission to receive notifications has been granted");
    }
  });
}

上面未经测试,因为我没有原始文件,但您应该明白这一点。我想补充一点,该分区也会提供截断值,因此不准确。