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

JavaScript

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:

JavaScript

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:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement