Home > Net >  How can I use awk (or sed) to put data set labels onto each line of the data sets?
How can I use awk (or sed) to put data set labels onto each line of the data sets?

Time:11-17

I have a bunch of data for different devices in a file and it's set up like this:

device: thing1

data1 data2 data3 data4

data1 data2 data3 data4

...

device: thing2

data1 data2 data3 data4

data1 data2 data3 data4

...

I need to format it like this:

thing1 data1 data2 data3 data4

thing1 data1 data2 data3 data4

...

thing2 data1 data2 data3 data4

thing2 data1 data2 data3 data4

I'm thinking awk is the way to go. The label "device:" appears every few hundred lines or so to indicate a data set from another device. So, I can match on that and put the second field into a variable. The problem is that I'm not sure how to match on it without excluding all the lines with the data. Here's what I've got so far:

-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

CodePudding user response:

Assumptions:

  • device: lines contain only 2 space-delimited strings (eg, device name does not contain white space)
  • do not print the device: lines
  • if there are blank lines then skip them
  • default output field separator (OFS) of a single space is sufficient for the resulting output

One awk idea:

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

This generates:

thing1 data1 data2 data3 data4
thing1 data1 data2 data3 data4
thing2 data1 data2 data3 data4
thing2 data1 data2 data3 data4

CodePudding user response:

sed -e "s/^\(.*\)/constant_fieldname \1/" filename

You could try something like this, to add something at the begging of each row

  • Related