Skip to content
Advertisement

Re-arranging lines after a pattern in a file according to a specific order

I have a large log file with the below format

date pattern1
time pattern2
variable1_name val1
variable2_name val2
variable3_name val3
variable4_name val4
date pattern1
time pattern2
variable1_name val1
variable2_name val2
variable3_name val3
variable4_name val4

I have created a shell script that insert those values in the database in the same order val1, val2, val3 ,val4

The problem is that the files sometimes gets corrupted and the variables come in different order, like below for example:

date pattern1
time pattern2
variable2_name val2
variable1_name val1
variable3_name val3
variable4_name val4
date pattern1
time pattern2
variable4_name val4
variable2_name val2
variable3_name val3
variable1_name val1

Using shell script, I want to rearrange the lines after pattern1 and pattern2 to be in the the same order as the original log file.

Advertisement

Answer

If the variable names are not in alphanumeric order (e.g. if variable1_name is “Nationality”, variable2_name is “Height”, and so on), then native sorting functions won’t work. But this will:

sed '/pattern2/{N;N;N;N;P;h;s/.*(variable1_name)/1/;P;g;s/.*(variable2_name)/1/;P;g;s/.*(variable3_name)/1/;P;g;s/.*(variable4_name)/1/;P;d;}' filename
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement