mardi 25 septembre 2018

Awk printing specific variable & original data together between the pattern

original data and desired output

Hello,

I want to get specific variable values between Patterns and print ( REF_ID value, C_ID value and Date-Time infront of it.

you can see Original data & Desired output below:

+--------+--------+-----+   +-------+--------+---+---+--------+--------+-----+
|        BEFORE         |   |                     AFTER  
+--------+--------+-----+    +-------+--------+---+---+--------+--------+-----+
| [2018  | 13:50] | ALI |    | [2018 | 13:50] | X | Y | [2018  | 13:50] | ALI |
| REF_ID | X      |     |    | [2018 | 13:50] | X | Y | REF_ID | X      |     |
| A      | 3      |     |    | [2018 | 13:50] | X | Y | A      | 3      |     |
| C_ID   | Y      |     |    | [2018 | 13:50] | X | Y | C_ID   | Y      |     |
| B      | 4      |     |    | [2018 | 13:50] | X | Y | B      | 4      |     |
| [2018  | 14:00] | ALI |    | [2018 | 14:00] | Z | T | [2018  | 14:00] | ALI |
| C      | 3      |     |    | [2018 | 14:00] | Z | T | C      | 3      |     |
| REF_ID | Z      |     |    | [2018 | 14:00] | Z | T | REF_ID | Z      |     |
| D      | 1      |     |    | [2018 | 14:00] | Z | T | D      | 1      |     |
| E      | 4      |     |    | [2018 | 14:00] | Z | T | E      | 4      |     |
| C_ID   | T      |     |    | [2018 | 14:00] | Z | T | C_ID   | T      |     |
+--------+--------+-----+    +-------+--------+---+---+--------+--------+-----+

I have tried the following but it does not work as i desired.

awk '
BEGIN {FS=" "}
{if ($0 ~ /\[2018/) {flag=1;date_ref=$1;time_ref=$2;}            }
{if ($0 ~ /REF_ID/ ) {t_ref=$2}                   }
{if ($0 ~ /C_ID/ ) {gcid_ref=$2}        }
{if (flag=1) print date_ref,time_ref,t_ref,gcid_ref,$0}
'

to solve the problem i believe have to put the data to buffer , collect the variable and merge it..

could you please help me to solve this problem? If you can explain the code you provide it would be much helpful.

Aucun commentaire:

Enregistrer un commentaire