Link to home
Start Free TrialLog in
Avatar of Watnog
WatnogFlag for Belgium

asked on

Bash: file manipulation/conversion

Dear Experts,

I hope you can help me with this manipulation of a file which needs to be re-designed so that it can be used in Graphviz.

Input file as below, chunk starts with "SCHEDULE" and ends with "END".
In between chunks there's a blank line.
In each of the chunks there is upper and lower half seperated by a ":".

In the upper part each line that start with "FOLLOWS" should come on 1 line with and preceeding "SCHEDULE", seperated with "->", double quotes are necessary...
                example: "FOLLOWS FTA_BES_ACC#SBP_BE_BEG_PRERQ.@"->"SCHEDULE FTA_BES_ACC#SBP_BE_BOOT_J2P0"

In the lower half each line should be preceeded by the "SCHEDULE" line (the first line of the chunk)
                example: "SCHEDULE FTA_BES_ACC#SBP_BE_BOOT_J2P0"->"FTA_BES_ACC#SBP_BE_BOOT_J2P0"

               
Input file:              
                               
               

                SCHEDULE FTA_BES_ACC#SBP_BE_BOOT_J2P0
                FOLLOWS FTA_BES_ACC#SBP_BE_BEG_PRERQ.@
                :
                FTA_BES_ACC#SBP_BE_BOOT_J2P0
                END

                SCHEDULE FTA_BES_ACC#SBP_BE_CHKV2SBP
                FOLLOWS FTA_BES_ACC#SBP_BE_BOOT_J2P0.@
                :
                FTA_BES_ACC#SBP_BE_CHKV2SBP_SYNC
                FTA_BES_ACC#SBP_BE_CHKV2SBP_SEND
                FTA_BES_ACC#SBP_BE_CHKV2SBP_LIST
                END

                SCHEDULE FTA_BES_ACC#SBP_BE_CHKNXCTRL
                FOLLOWS FTA_BES_ACC#SBP_BE_CHKV2SBP.@
                FOLLOWS FTA_BES_ACC#SBP_BE_BEG_PRERQ.@
                :
                FTA_BES_ACC#SBP_BE_CHKNXCTRL_MONITOR_CHECK2
                FTA_BES_ACC#SBP_BE_CHKNXCTRL_MONITOR_CHECK
                END
Output file:

               "FOLLOWS FTA_BES_ACC#SBP_BE_BEG_PRERQ.@"->"SCHEDULE FTA_BES_ACC#SBP_BE_BOOT_J2P0"
                "SCHEDULE FTA_BES_ACC#SBP_BE_BOOT_J2P0"->"FTA_BES_ACC#SBP_BE_BOOT_J2P0"

                "FOLLOWS FTA_BES_ACC#SBP_BE_BOOT_J2P0.@"->"SCHEDULE FTA_BES_ACC#SBP_BE_CHKV2SBP"
                "SCHEDULE FTA_BES_ACC#SBP_BE_CHKV2SBP"->"FTA_BES_ACC#SBP_BE_CHKV2SBP_SYNC"
                "SCHEDULE FTA_BES_ACC#SBP_BE_CHKV2SBP"->"FTA_BES_ACC#SBP_BE_CHKV2SBP_SEND"
                "SCHEDULE FTA_BES_ACC#SBP_BE_CHKV2SBP"->"FTA_BES_ACC#SBP_BE_CHKV2SBP_LIST"

                "FOLLOWS FTA_BES_ACC#SBP_BE_CHKV2SBP.@"->"SCHEDULE FTA_BES_ACC#SBP_BE_CHKNXCTRL"
                "FOLLOWS FTA_BES_ACC#SBP_BE_BEG_PRERQ.@"->"SCHEDULE FTA_BES_ACC#SBP_BE_CHKNXCTRL"
                "SCHEDULE FTA_BES_ACC#SBP_BE_CHKNXCTRL"->"FTA_BES_ACC#SBP_BE_CHKNXCTRL_MONITOR_CHECK2"
                "SCHEDULE FTA_BES_ACC#SBP_BE_CHKNXCTRL"->"FTA_BES_ACC#SBP_BE_CHKNXCTRL_MONITOR_CHECK"
               

Please be so kind to check into, this is above my skill level.
Thanks in advance.
W.
Avatar of noci
noci

Save the second code block as xform.sh
and run
bash xform.sh inputfile outputfile

Open in new window


#!/bin/bash
IN=$1
OUT=$2
SCHED=""
fase=up
>$2             # empty output
while read line
do
        if [ "$line" != "" ]
        then
                if [ "$line" == "END" ]
                then
                        SCHED=""
                        fase=up
                        echo "" >>$OUT
                elif [ "$line" == ":" ]
                then
                        fase=dn
                else
                        left=${line##SCHEDULE}
                        if [ "$left" != "$line" ]     # Schedule is removed --> SCHEDULE LINE
                        then
                                SCHED=$line
                        else
                                if [ "$fase" == "up" ]
                                then
                                        echo "\"${line}\"->\"${SCHED}\"" >>$OUT
                                else
                                        echo "\"${SCHED}\"->\"${line}\"" >>$OUT
                                fi
                        fi
                fi
        fi
done <$IN

Open in new window

Avatar of Watnog

ASKER

Looks very good Noci.
Let me check things out further and I'll get back to you (my CET tomorrow).
Thanks and cheers.
W
Avatar of Watnog

ASKER

Hi Noci.

What you gave me works fine, thank you for that.
However it is slightly more complicated...

Below an example where there's a FOLLOWS also in the lower part of the chunk.


SCHEDULE FTA_BES_ACC#SBP_BE_CHKV2SBP
FOLLOWS FTA_BES_ACC#SBP_BE_BOOT_J2P0
:
FTA_BES_ACC#SBP_BE_CHKV2SBP_SYNC
FTA_BES_ACC#SBP_BE_CHKV2SBP_SEND
 FOLLOWS SBP_BE_CHKV2SBP_LIST
FTA_BES_ACC#SBP_BE_CHKV2SBP_LIST
END

The  chunk is different in these lines:

FTA_BES_ACC#SBP_BE_CHKV2SBP_SEND
 FOLLOWS SBP_BE_CHKV2SBP_LIST

The idea remains the same: follows is on left side of the arrow but the FOLLOW is ommited:
"FTA_BES_ACC#SBP_BE_CHKV2SBP_LIST"->"FTA_BES_ACC#SBP_BE_CHKV2SBP_SEND"

A secondary thing is that in the lower part the "FTA_BES_ACC#" is omitted too.
It would be great if you could have that added: it is same as the first part of the SCHEDULE name.


Final result:

"SCHEDULE FTA_BES_ACC#SBP_BE_CHKV2SBP"->"FTA_BES_ACC#SBP_BE_CHKV2SBP_SYNC"
"FTA_BES_ACC#SBP_BE_CHKV2SBP_LIST"->"FTA_BES_ACC#SBP_BE_CHKV2SBP_SEND"
"SCHEDULE FTA_BES_ACC#SBP_BE_CHKV2SBP"->"FTA_BES_ACC#SBP_BE_CHKV2SBP_LIST"

I hope this makes sense and that you can find some time to do your magic again.
Cheers.
Is this a constant: "FTA_BES_ACC#"  ? or should it come from a some identifyable line...
This is an illogical extention... because it is TRAILING the info it is used on... not impossible....
Avatar of Watnog

ASKER

The info is ommited in the input meaning that it is the same as in the line above...

FTA_BES_ACC#SBP_BE_CHKV2SBP_SEND
 FOLLOWS SBP_BE_CHKV2SBP_LIST

equals

FTA_BES_ACC#SBP_BE_CHKV2SBP_SEND
 FOLLOWS FTA_BES_ACC#SBP_BE_CHKV2SBP_LIST
ASKER CERTIFIED SOLUTION
Avatar of noci
noci

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Watnog

ASKER

Great Noci, it does exactly what I need...
I attach output after been processed by Graphviz.

It should miss the schedule but refer to the job it "follows" instead, and I left out input that worked fine already.

Thanks a bunch. I couldn't have come up with this, far from.
Cheers and have a good weekend.
W
sample.jpg
Avatar of Watnog

ASKER

Thanks for your responsiveness and efficient help.