Abhinav.K
Abhinav.K

Reputation: 179

Take the last word from a line and add it at the beginning using BASH

we have a requirement where contents of our text files are like this:

[some-section-1]
big_msg_line1   var=random_value1
big_msg_line2   var=random_value2
big_msg_line3   var=random_value3

[some-section-2]
"lots of irrelevant data"

[some-section-3]
"lots of irrelevant data"

[some-section-4]
big_msg_line4   var=random_value4
big_msg_line5   var=random_value5
big_msg_line6   var=random_value6
big_msg_line7   var=random_value7
big_msg_line8   var=random_value8

[some-section-5]
"lots of irrelevant data"

All the lines that we want to modify starts with common charaters, like in this example all lines which we would like to modify starts with the word "big". We would like to change it to something like this:

[some-section-1]
random_value1   msg=big_msg_line1
random_value2   msg=big_msg_line2
random_value3   msg=big_msg_line3

[some-section-2]
"lots of irrelevant data"

[some-section-3]
"lots of irrelevant data"

[some-section-4]
random_value4   msg=big_msg_line4
random_value5   msg=big_msg_line5
random_value6   msg=big_msg_line6
random_value7   msg=big_msg_line7
random_value8   msg=big_msg_line8

[some-section-5]
"lots of irrelevant data"

These were for examples only. The original file contains way lot more data than these. In hundreds if not in thousands lines.

I am currently doing this using for a loop, reading each line, cutting the values, formatting them like the way I want, putting then in separate file and then replace the original file with the new file. Is there a way to achieve this using some one liners? That would really be of great help. Hope I am clear with my question.

Thanks in advance.

Upvotes: 1

Views: 110

Answers (2)

jrjc
jrjc

Reputation: 21873

From what I understood, this awk one-liner would do the job :

cat a
[some-section-1]
big_msg_line1   var=random_value1
big_msg_line2   var=random_value2
big_msg_line3   var=random_value3

[some-section-2]
lots of irrelevant data

[some-section-3]
lots of irrelevant data

[some-section-4]
big_msg_line4   var=random_value4
big_msg_line5   var=random_value5
big_msg_line6   var=random_value6
big_msg_line7   var=random_value7
big_msg_line8   var=random_value8

[some-section-5]
lots of irrelevant data 

This :

awk '{FS="var="; if ($1~/big/) { print $2"\tmsg="$1} else {print }}' a

Gives

[some-section-1]
random_value1   msg=big_msg_line1   
random_value2   msg=big_msg_line2   
random_value3   msg=big_msg_line3   

[some-section-2]
lots of irrelevant data

[some-section-3]
lots of irrelevant data

[some-section-4]
random_value4   msg=big_msg_line4   
random_value5   msg=big_msg_line5   
random_value6   msg=big_msg_line6   
random_value7   msg=big_msg_line7   
random_value8   msg=big_msg_line8   

[some-section-5]
lots of irrelevant data 

Upvotes: 3

Dan Graller
Dan Graller

Reputation: 325

this command should do the job

 sed -e 's/\(big[^ ]*\)\([ ]*\)var=\([^ ]*\)/\3\2msg=\1/' [your file] > [output file]

EDIT: You might need to change the slahes (/) to a letter which is not used in your file

Upvotes: 2

Related Questions