繁体   English   中英

如何使用 awk(或 sed)将数据集标签放在数据集的每一行上?

[英]How can I use awk (or sed) to put data set labels onto each line of the data sets?

我在一个文件中有一堆针对不同设备的数据,它的设置如下:

device: thing1

data1 data2 data3 data4

data1 data2 data3 data4

...

device: thing2

data1 data2 data3 data4

data1 data2 data3 data4

...

我需要像这样格式化它:

thing1 data1 data2 data3 data4

thing1 data1 data2 data3 data4

...

thing2 data1 data2 data3 data4

thing2 data1 data2 data3 data4

我在想 awk 是通往 go 的路。label“设备:”每隔几百行左右出现一次,表示来自另一个设备的数据集。 所以,我可以匹配它并将第二个字段放入一个变量中。 问题是我不确定如何在不排除所有数据行的情况下匹配它。 这是我到目前为止所得到的:

-bash-4.2$ awk '/device:/{device=$2; print device, $0;}' data_sets.txt | head -n 10

thing2 device: thing2

thing3 device: thing3

thing6 device: thing6

thing7 device: thing7

another_thing0 device: another_thing0

another_thing1 device: another_thing1

thing2 device: thing2

thing3 device: thing3

thing6 device: thing6

thing7 device: thing7

假设:

  • device:行仅包含 2 个空格分隔的字符串(例如,设备名称不包含空格)
  • 不打印device:线
  • 如果有空行则跳过它们
  • 单个空格的默认 output 字段分隔符 ( OFS ) 足以生成 output

一个awk想法:

awk '
/^device:/ { device=$2; next }          # make note of our new device name; skip to next line of input
NF > 1     { print device,$0 }          # if line is not blank/empty then print the label and the current line of input
' data_file.txt

这会产生:

thing1 data1 data2 data3 data4
thing1 data1 data2 data3 data4
thing2 data1 data2 data3 data4
thing2 data1 data2 data3 data4
sed -e "s/^\(.*\)/constant_fieldname \1/" filename

你可以尝试这样的事情,在每一行的开头添加一些东西

这可能对你有用(GNU sed):

sed -E '/^constant_fieldname: \S+$/{h;d};G;s/^(.*)\n\S+: (\S+)$/\2 \1/' file

将常量复制到保留空间,然后删除该行。

对于所有其他行,append 是当前行的常量,并使用替换重新排列格式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM