ksh script

Hello all
I'm writing a ksh script in which I'm stuck. I'm reading from a file whith the following information:
23
0001
0003
0004
0006
4
0000
0004
0005
0006
3
0000
0006
0008
0009
0011
0100

I would like to read in the file and ignore the number with out a preceeding 0 and then search for one number and get the preceeding number. example

I want 0005 which mean I would like 0000 0004 from the second group of number. So my result would be:
0000
0004
0005

I can get the read in file part but suck on how to comapre the values. I thought of using an array and store the value but I don't want to store everything preceeding ie. 23 0001 0003 0004 ....

Any help would be great. THANKS in advance

I'm not exactly sure what you want to pull from the file.. if you want to pull any line out that starts with a zero, you can use:

sed -n -e '/^0/p' < file2

I kinda get the impression you want to do more, but I don't know exactly what.. maybe if you can explain the problem differently...

Thanks oombera

You're rigth I want to do more. I just don't only want the non zero number but I would like only the nonzero number of the group. Example first group would be: (base on example given)

23
0001
0003
0004
0006

the second group:
4
0000
0004
0005
0006

the thrid group:
3
0000
0006
0008
0009
0011
0100

Now I want number 0005 which is in the second group and its preceeing number in that group .i.e 0004 0000 but not 0006 or any other.

running the script > script_name 0005
will produce
0000
0004
0005

I hope this helps. THANKS for your input.

How about
sed -ne '/^0/p' -e '/0005/q' < inputfile | tail -3

And here I was beginning some big, elaborate script.. I didn't think of taking the output of sed and feeding it back into sed.. wow :rolleyes:

The only thing extra that I assumed the OP wanted was possibly to search for other numbers, such as 0003 or 0009, in which case the number in the tail command would have to be changed.. but I guess that depends on what larry wants.

Perderabo was the UNIX.COM poster (Member) of the year in 2002. He is simply amazing!

Neo

Thank Perderabo and oombera

But the group can chage in size example group 2 could be

0000
0001
0003
0004
0005
0006

thus tail 3 would only work if I knew the the total number in that group. But I don't know, like in the above example if I use tail -3 I would only get :

0003
0004
0005

and lose 0001 0000

Thanks again

Thank you all, for your respones. After days of headache I found a solution; I just creat a array and put every thing in until I get to the number I'm looking for and then just take it back out (in reverse order) until I get to a 0000 number in which I stop. It would be easy in C or C++ with a stack( using push and pop ) but in script there isn't any push or pop that I know of. If any one would like to look at the script. Let me know I will post it. Thanks again

HI
I think you are correct. Please post your shell script.I want to have a look at your shell script.

What my suggestion is store the size of the array in a variable and display it till you reach a specified number.

Here is the script. I hope this is helpful to everyone.

#!/bin/ksh
#
#
#Getting information into the array

index=0
look=$1
while read snumber
do
   if [ $snumber = $look ]
   then
       c_array[$index]="$snumber"
       break
  else
       c_array[$index]="$snumber"
       index=`expr $index + 1`
  fi
done < file_name


#Getting information out of the array

indeo=${#c_array[*]}
while (( $indeo > 0 ))
do
   if [[ ${c_array[$indeo]} = "0000" ]]
   then
      echo ${c_array[$indeo]}
      break
  else
      echo ${c_array[$indeo]}
      indeo=`expr $indeo -1`
   fi
done

exit 0
###########

this is works for me but if anyone see any improvement please tell me.

I added code tags for readability...Perderabo

If raw ksh is required, I guess the OP's final solution was fine. In awk:

nawk -v sought=0005 '/^[1-9]/ { i=0 }
/^0/ { a[++i] = $0 }
$1 == sought { for (j=1; j<=i; j++) print a[j] }'

This restarts accumulating lines whenever the number on a line doesn't start with zero. It gathers all the lines that start with zero, then when it sees the number "sought", it prints the numbers that have accumulated since the last non-zero.

If a group contains more than one copy of the number sought, you may not get the expected result. There are other gotchas as well, to get around them requires more code and some knowledge about the shape of your data. More work is also required if there is more than one group that contains your number and you want a blank line between, etc.