参加考试并粘贴在行内

huangapple go评论63阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年3月7日 19:16:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661290.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定