脚本中的bash行迭代

脚本中的bash行迭代,bash,text,iteration,Bash,Text,Iteration,我在文本文件中有以下数据 INSTANCES machine1 Name Berlin Role NA INSTANCES machine2 Name London Role NA INSTANCES machine3 Role parser Name Dublin INSTANCES machine4 Name Madrid INSTANCES machine5 Role parser Name

我在文本文件中有以下数据

INSTANCES machine1    
Name    Berlin
Role    NA
INSTANCES  machine2   
Name    London
Role    NA
INSTANCES machine3       
Role    parser
Name    Dublin
INSTANCES machine4       
Name    Madrid
INSTANCES machine5
Role    parser
Name    Lisbon
我需要创建以下输出。我该怎么做

machine1    Berlin    NA
machine2    London    NA
machine3    Dublin    parser
machine4    Madrid    NONE
machine5    Lisbon    parser
这应该做到:

#!/bin/bash
count=0
while read -r line; do
arr=($line)
if [[ $line == *INSTANCES* ]]; then
[[ $count != 0 ]] && new+="\n"${arr[1]} || new+=${arr[1]}
else
new+=" ${arr[1]}"
fi
((count++))
done <file
new+="\n"
printf "$new"

file是文本文件名

使用linux上可用的许多程序中的任意一个。您可能已经安装了awk、sed和perl。这些可以用于基本的文本操作。标记应该只用于有关处理语言的问题。