2015-04-28 67 views
-4

我在文本文件中的以下数据庆典线迭代脚本

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 
+2

使用Linux平台上的一些方案中的任何一个。您可能安装了awk,sed和perl。这些可用于此基本文本操作。 – 2015-04-28 13:18:49

+0

[tag:processing]标签只能用于有关Processing语言的问题。 –

回答

0

这应做到:

#!/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" 

文件是文本文件名