Script - Filter data - repeated loop - Help needed...

Dear Friend,

I need a help for the below problem.. Can anyone suggest me to do...

Input file data:

rule {
name=mainrule
filt=pos
loc {
x=right + 660
y=top - 3100
}
object_kind= DRAW
object_name= pen
sym_name_expr= 123
anchor= 0
pol= 1
rot= 0
angle=0
mirror=no

      text \{
          x_size=0
          y_size=0
          width=0
      \}

}

rule {
name=rule2
filt=neg
loc {
x=890
y=100
}
object_kind= shape
object_name= ink
sym_name_expr= 321
anchor= 1.5
pol= 0
rot= 90
angle=90
mirror=yes

      text \{
          x_size=1
          y_size=1
          width=1
      \}

}

In the above file, the loop and sub-loop has variables and values. Now i need the output as for all variables only the values, like a report.

Required output:

Rule
Name mainrule rule2
Filt pos neg
Loc
X right + 660 890
Y top - 3100 100
Object_kind DRAW shape
Object_name pen ink
sym_name_expr 123 391
anchor 0 1.5
pol 1 0
rot 0 90
angle 0 90
mirror no yes
text
x_size 0 1
y_size 0 1
width 0 1

I have tried in awk but, loop-wise how to filter the data's. :confused:

Thanks in Advance,
Vasanth

-----Post Update-----

The below problem solution required.....

-----Post Update-----

Anybody have answer for my Question

-----Post Update-----

Anybody have answer for my Question

awk -F"[{=}]" '{ a[$1]=a[$1]" "$2;next } END { for(i in a) print i  a }' input_file.txt

check the forum to print the array elements in order, since the order of the array elements wont be the same as the order in which we processed.

Dear Panyam,

The code given by u, is scanning file and giving output by the repeated lines.

The required output should be variable should remain and the values should come in the same row..

Thanks in advance
Vasanth

This was really challenging Vasanth...

test=testfile
count=1
for i in `awk '{FS="[{}= \t]+";print $1}' $test`
do
   a=`grep -w $i $test | awk '{FS="[{}= \t]+";for (i=2;i<=NF;i++)print $i}' | xargs`
   if [[ $i == "rule" && $count -ne 1 ]];then
      break
   fi
   echo $i $a
   (( count = $count + 1 ))
done
awk -F"[{=}]" '!($1 in A) {key[++key[0]]=$1} {a[$1]=a[$1]" "$2;next} END { for(i=1;i<=key[0]/2;i++){ print key a[key]}}'  | sed '/^[ ]*$/d'  input_file.txt

Above worked fine for me
here is the output i got for your input file

rule
name mainrule rule2
filt pos neg
loc
x right + 660 890
y top - 3100 100
object_kind  DRAW  shape
object_name  pen  ink
sym_name_expr  123  321
anchor  0  1.5
pol  1  0
rot  0  90
angle 0 90
mirror no yes
text
x_size 0 1
y_size 0 1
width 0 1

Dear rakeshawasthi,

Thanks. Its look like bash shell script.

Is it possible using CSH.

Thanks
Vasanth

I think it will work...try :slight_smile:

Dear Rakeshawasthi,

I tried the below code in Bash shell, but i am getting error as "syntax error near unexpected token '$'do\r'' .

kindly clarify.....

Code used:

test=testfile
count=1
for i in `awk '{FS="[{}= \t]+";print $1}' $test`
do
a=`grep -w $i $test | awk '{FS="[{}= \t]+";for (i=2;i<=NF;i++)print $i}' | xargs`
if [[ $i == "rule" && $count -ne 1 ]];then
break
fi
echo $i $a
(( count = $count + 1 ))
done

Thanks in advance
vasnath

-----Post Update-----

Dear panyam,

i have used the below code, which you referred....

awk -F"[{=}]" '!($1 in A) {key[++key[0]]=$1} {a[$1]=a[$1]" "$2;next} END { for(i=1;i<=key[0]/2;i++){ print key [i]a[key[i]]}}' | sed '/^[ ]*$/d' input_file.txt

I am getting the output, same as input. It is working just like cat command.

Kindly clarify......
Vasanth

sorry there was a typo .

awk -F"[{=}]" '!($1 in a) {key[++key[0]]=$1} {a[$1]=a[$1]" "$2;next} END { for(i=1;i<=key[0];i++){ print key a[key]}}' input_file_name.txt |  sed '/^[ ]*$/d'  

Yes, thanks a lot, Panyam....

It works for me.
have you replaced the testfile with your input file name..
tell me how can I replicate the error?

One more thing, panyan i need a scritp to remove the space before and after the operator like( +, -, ||, &&).

Ex :

Input file

apple + manago

mango && fresh + apple fresh || fruit

Desired output:

apple+manago

mango&&fresh+apple fresh||fruit

Note: betwee the desired operator space should be removed, between words do not remove space.

Thanks in advance,
Vasanth

-----Post Update-----

Dear rakeshawasthi ,

I have used cygwin bash shell.

I think your code, u r executing in KSH.

code used :

test=testfile
count=1
for i in `awk '{FS="[{}= \t]+";print $1}' $test`
do
a=`grep -w $i $test | awk '{FS="[{}= \t]+";for (i=2;i<=NF;i++)print $i}' | xargs`
if [[ $i == "rule" && $count -ne 1 ]];then
break
fi
echo $i $a
(( count = $count + 1 ))
done

Error message:
syntax error near unexpected token '$'do\r''

Thanks
vasanth

-----Post Update-----

Dear rakeshawasthi ,

I have used cygwin bash shell.

I think your code, u r executing in KSH.

code used :

test=testfile
count=1
for i in `awk '{FS="[{}= \t]+";print $1}' $test`
do
a=`grep -w $i $test | awk '{FS="[{}= \t]+";for (i=2;i<=NF;i++)print $i}' | xargs`
if [[ $i == "rule" && $count -ne 1 ]];then
break
fi
echo $i $a
(( count = $count + 1 ))
done

Error message:
syntax error near unexpected token '$'do\r''

Thanks
vasanth

Yes .

Ok.rakeshawasthi ,

do you answer for my new post, about removing spaces inbetween operators

Thanks in advance
Vasanth

Vasanth,
see my hint in your new thread.

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums