我有一个脚本,它给出如下输出:
ruby-devel is needed by software1
tcl is needed by software 2
python3 is needed by software 3
ocaml is needed by software 1
我是 awk 的新手,但试图编写脚本来获取第一个单词,并将其放在一行中(可以使用 sed 或实际执行此操作的最佳方法,但我做不到)以便能够构建如下输出:
You need to get: ruby-devel tcl python3 ocaml
Run: yum install ruby-devel tcl python3 ocaml
关于如何做到这一点有什么帮助吗?
答案1
假设您正在使用 bash,像这样?
WORDS=$( your_script | awk '{printf("%s ",$1);}' )
printf 'You need to get: %s\n' "${WORDS}"
printf 'Run: yum install %s' "${WORDS}"
答案2
awk
awk '{if ($1~/ruby|tcl|python3|ocaml/) $1="ruby-devel tcl python3 ocaml" } END { print "You need to get:", $1, "\nRun: dnf install", $1 }' $file