shell script to format file based on specific patterns

Please help me out and drag me out the deadlock I am stuck into:

I have a file. I want the statements under a if...then condition be listed in a separate file in the manner condition|statement.Following are the different input pattern and corresponding output parameters.any generic code to handle all these would be very very helpful.

Case:1 INPUT
------
if cond1 = true then
parm1=a
parm2=b
parm3=c
if cond2 = true then
parm3=d
parm4=e
end if
end if

OUTPUT
-------
cond1|parm1
cond1|parm2
cond1|parm3
cond2|parm3
cond2|parm4

Case:2 INPUT
------
if cond1 = true then
if cond2 = true then
parm1=a
parm2=b
parm3=c
parm3=d
parm4=e
end if
end if

OUTPUT
-------
cond1|cond2|parm1
cond1|cond2|parm2
cond1|cond2|parm3
cond1|cond2|parm3
cond1|cond2|parm4

Case:3 INPUT
------
if cond1 = true then
if cond2 = true then
end if
parm1=a
parm2=b
parm3=c
parm3=d
parm4=e
end if

OUTPUT
-------
cond1|parm1
cond1|parm2
cond1|parm3
cond1|parm3
cond1|parm4

what have you tried till now??

Can you explain just a little more?

You mention you want the statements to be in a separate file. Are you wanting to read or write to the file? Are these multiple files or a single? Are the parameters command line options or a menu response?

Without the code logic can you explain what you're looking for as far as files and the script command line?

Sorry for all the questions, but the general if..then of the code examples is self-explanatory, but I'm not understanding the ultimate goal here.

okay, the intent is to write to a file ( specified as "OUTPUT" ) in original post from the file ( specified as "INPUT" ). There are no command line options or a menu response. Everything is file content and can be treated as records of the file. Did this answer your question ?

I am really new to awk and cannot figure it out where I am making the mistake. I tried to handle a situation in which the input file looks like this:

file.txt
------
cond=cond1
cond=cond2
parm=parm1
parm=parm2
parm=parm3
cond=cond3
parm=parm4
parm=parm5

The script which I wrote is given below but it hangs:'

awk '{ x=1
b=0
while ( $x <= NF ) {
if ( $x ~ /cond=/ ) {
if b = 1 {
cond = "" }
cond = cond "|" $x }
else if ( $x ~ /parm=/ ) {
b = 1
parm = cond "|" parm "|" $x
print parm }
x++
}
}' file.txt

I would like to have a generic solution for all the cases.

Yes and No. Can you provide a sample of the input file?

From what I understand so far, a script reads in each line of an input file, then writes to an output file based on the if conditions. Will all 3 cases be within the same script? Will the output be written to the same file and combined in some way or different files? Do you have a preference in language? (bash, perl, ...)

psuedo code for case 1:

# assuming inputfile format of "value1 value2"
while read -u inputfile var1 var2
do
  if condition1; then
    parm1=a
    parm2=b
    parm3=c
    if condition2; then
      parm3=d
      parm4=e
    fi
    for num in {1..4}
    do
      printf "cond1=%s|cond2%s|parm%s=%s\n" $cond1 $cond2 $num ${parm${num}} >> outputfile
    done
fi

Probably my question is not clear. Here is what I want to do. I want to parse a file with if then....end if blocks. I want to have the if condition added in front of the statements which are within the if..end if block e.g.

in the simplest form

if cond1 then
statement1
statement2
end if

So I want a new file of the form:
cond1 | statement1
cond1 | statement2

So, now if we have a nested if...end if block
if cond1 then
if cond2 then
statement1
statement2
end if
end if

So the new file should have:

cond1|cond2|statement1
cond1|cond2|statement2.

Did it explain the problem scenario ? Is it possible using korn shell ?

Sorry I didn't respond sooner, but I've been busy at work. I'm not big on Korn, but Bash gets many of it's features from it.

I've written two scripts, one in Bash and the other in Perl. Each contains 3 different cases and outputs the data belowI'm not quite sure if I understood the need completely, but I gave it a shot. If it's not what you were looking for I would need please post a copy of the real input file that you're wanting to process.

Because the scripts are large from putting multiple examples in each, I have made them available at the following page rather than try to include them here. You can find them at script examples ?(bwhitehd)?

I took a look at the awk example you provided. Part of the reason it didn't work is that you are missing several '$' in front of some of the variables.

I hope this helps. If you have any specific questions, let me know.

  • B