我有这个文件(output.txt):
name:some name^M
^M
count:1^M
^M
region:some_region
name:some name^M
^M
count:2^M
^M
region:some_region^M
^M
name:some name^M
^M
count:1^M
^M
region:some_region^M
^M
Testing Powershell scripts execustions from Rundeck server against Windows server
如何仅获取名称、计数和区域后的值
期望输出:
some name 1 some region
some name 2 some region
some name 1 some region
和 grep -E 'name|count|region' output.txt | sed 's/^.*: //'
我有
some name
1
some_region
some name
2
some_region
some name
1
some_region
如何生成多列?
答案1
使用 awk 的一个可能的答案:
cat input | awk '/name: /{name=substr($0,7)} /count: /{cnt=substr($0,8)} /region:/{print name" "cnt" "substr($0,9)}'
答案2
awk '/^(name|count|region):/{
sep = (/^region/? ORS : OFS);
gsub(/^[^:]+:[[:space:]]*|[[:space:]]*$/, "");
printf "%s%s", $0, sep
}' file.txt
感谢@RomanPerekhrest