如何用 echo 分隔 awk print 中的 2 個變數?

如何用 echo 分隔 awk print 中的 2 個變數?

我做了一個小測試來回顯我的 .txt 命名入口網站中的所有內容。在終端中寫入時,我希望腳本回顯所有輸入的名稱,但我只顯示第一個變數。

input=$1

  for portal in $(grep $input /etc/portals | sed '/^#/ d' | awk '{print $1, $2}');

  do
    echo -e "\e[1;32m "$portal" \e[0m";
    exit 0
    done

else
    echo -e "\e[1;31m --> Wrong Input <-- \e[0m"
    exit 1
    done

fi

=============

該代碼可以工作,但只列印 $1 而不是 $2。如果我將程式碼更改為: awk '{print $1 $2}' 則輸出為 $1$2,變數之間沒有製表符或空格。 =====================================

如何分離變數以便顯示迴聲:

測試1 [製表符/空格] 測試2

答案1

#!/bin/bash

# exit if input is empty.
[[ -z $1 ]] && exit 1

# Check in the file /etc/portals for 
# the existens of the word "$1" and place 
# first word in "$a" and rest in "$b".
while read -r a b; do
    printf %s\\t%s\\n $a "$b" 
done < <(grep -P "^(?!#).*$1" /etc/portals)

相關內容