Linux

如何使用 awk 將字元串添加到輸出的第一個欄位?

  • August 21, 2017

以下腳本列印輸出:

/var/lib/ambari-server/resources/scripts/configs.sh get localhost c1 yarn-env

腳本的輸出:

"yarn_log_dir_prefix" : "/var/log/hadoop-yarn",
"yarn_pid_dir_prefix" : "/var/run/hadoop-yarn",
"yarn_user" : "yarn",
"yarn_user_nofile_limit" : "32768",
"yarn_user_nproc_limit" : "65536"

我想用管道添加awk或perl oneline線性,輸出的第一個欄位中的yarn-env如下:(預期結果)

/var/lib/ambari-server/resources/scripts/configs.sh get localhost c1 yarn-env | awk ...


yarn-env  "yarn_log_dir_prefix" : "/var/log/hadoop-yarn",
yarn-env  "yarn_pid_dir_prefix" : "/var/run/hadoop-yarn",
yarn-env  "yarn_user" : "yarn",
yarn-env  "yarn_user_nofile_limit" : "32768",
yarn-env  "yarn_user_nproc_limit" : "65536"
/var/lib/ambari-server/resources/scripts/configs.sh get localhost c1 yarn-env |
awk '{ print "yarn-env", $0 }'

這個簡短awk的腳本會將字元串yarn-env添加到命令的每一行輸入中。使用的分隔符是awk變數OFS(“輸出欄位分隔符”,預設情況下為單個空格),因此如果您想要兩者之間的製表符,請使用

/var/lib/ambari-server/resources/scripts/configs.sh get localhost c1 yarn-env |
awk -vOFS="\t" '{ print "yarn-env", $0 }'

或者,使用sed

/var/lib/ambari-server/resources/scripts/configs.sh get localhost c1 yarn-env |
sed 's/^/yarn-env /'

引用自:https://unix.stackexchange.com/questions/386933