英文:
Take an exam and paste in line
问题
I have some issue regarding data processing using shell script. I have data like this
Instance: A
x.x.x.x rmt up
x.x.x.x rmt down
x.x.x.x rmt up
Instance: B
x.x.x.x up
I want to output using shell script like this
A x.x.x.x rmt up
A x.x.x.x rmt down
A x.x.x.x rmt up
B x.x.x.x up
I was trying but result not as expected:
using cat temp grep -i instance | sed 's/.*: //' : result only
A
B
Thanks before
英文:
I have some issue regarding data processing using shell script. I have data like this
Instance: A
x.x.x.x rmt up
x.x.x.x rmt down
x.x.x.x rmt up
Instance: B
x.x.x.x up
I want to output using shell script like this
A x.x.x.x rmt up
A x.x.x.x rmt down
A x.x.x.x rmt up
B x.x.x.x rmt up
I was trying but result no as expected:
using cat temp grep -i instance | sed 's/.*: //' : result only
A
B
Thanks before
答案1
得分: 2
您可以使用`awk`来实现此目的:
```sh
awk '/^Instance/ {ins = $2; next} NF {print ins,$1,$2,$3}' file
A x.x.x.x rmt up
A x.x.x.x rmt down
A x.x.x.x rmt up
B x.x.x.x up
英文:
You may use awk
for this:
awk '/^Instance/ {ins = $2; next} NF {print ins,$1,$2,$3}' file
A x.x.x.x rmt up
A x.x.x.x rmt down
A x.x.x.x rmt up
B x.x.x.x up
答案2
得分: 1
这是一个[tag:awk]的任务:
A x.x.x.x rmt up
A x.x.x.x rmt down
A x.x.x.x rmt up
B x.x.x.x up
请注意,$1=$1
重新排列了多余的空格。
英文:
It's a task for [tag:awk]:
$ awk '$1 == "Instance:"{val=$2;next} NF{$1=$1; print val, $0}' file
A x.x.x.x rmt up
A x.x.x.x rmt down
A x.x.x.x rmt up
B x.x.x.x up
Note that $1=$1
re-arrange the exceeded spaces
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论