shell script

if i do this 5 times
env >> xx
env >> xx
env >> xx
env >> xx
env >> xx
il will a file called XX with the env redirected into it 5 times

i need to create a script that takes 1 argument being a file, in this instancei ll use the newly created file above xx read the inputted file, in this case xx and look for occurrences of the current user who is executing the script. On finding an occurrence of the username take that line and append it to a file and display a line number and a bracket against the saved line.
The newly created file should look similar to this
1) PWD=/home/lee.ballancore
2) USER=lee.ballancore
3) MAIL=/var/spool/mail/lee.ballancore
4) LOGNAME=lee.ballancore
5) HOME=/home/lee.ballancore
6) PWD=/home/lee.ballancore
7) USER=lee.ballancore
8) MAIL=/var/spool/mail/lee.ballancore
9) LOGNAME=lee.ballancore
10) HOME=/home/lee.ballancore

As a starting point, try :

env | awk -v User=$USER '$0 ~ User { printf("%d) %s\n", ++count, $0) }'

Hi,
Check if this is of help.

#!/bin/ksh
sed -n "/USER=${USER}/,/USER=/p" /tmp/xx | cat -n  | sed 's/\([0-9]\{1,3\}\)/\1)/g' > /tmp/${USER}-env

Thanks
Nagarajan Ganesan

been told not to use sed nither awk...that would have been easy!!!

Why not use awk/sed ? Is it homework ?

The logic :

Initialize counter (value 0)
Display the current  environment variables
and For each line do
   If the line contains le username then
      increment the counter
      display counter and line.

You can do that with the commands :
case echo env expr for if read

Ciro,
As Vino said, if this is a homework, besides breaking this site rules,
you are cheating yourself.
The only way you will learn is to practice.
If this is not a homework:

grep "`whoami`" input_file | cat -n

I@M WORKING ON IT ON MY OWN!!!!!!!

this is what iv done and it looks good

[ciro.ruggiero@unix ~]$ grep $USER xx | cat -n

and this is part of the output:

 1  USER=ciro.ruggiero
 2  MAIL=/var/spool/mail/ciro.ruggiero
 3  PATH=/u01/app/oracle/product/10.2.0/db_1/bin:/usr/sbin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/ciro.ruggiero/bin
 4  PWD=/home/ciro.ruggiero
 5  HOME=/home/ciro.ruggiero
 6  LOGNAME=ciro.ruggiero

two questions...AND i@M JUST ASSKING F|OR AN HINT!!!!!!!!

How do i get rif of the 3th line and i do i put a bracket after the number?

grep $USER FILE | awk ' NR == 3 { getline; NR=3; } { print NR ") " $0 }

how do i put the brackets against the number
eg:
1)
2)

Did you try the above code?

You will not learn anything if you don't try to resolve the problems by yourself before posting.

Read the grep man pages

As you don't want to use awk nor sed, you can read the output of the cat command with the read command in a while loop.
The read command permits to read the first word in a variable and the rest of the line in another one.
RTFM.

this is the output i was looking for!!!!
just need to add a bracket on ech number and i do not know how to do it
[ciro.ruggiero@unix ~]$ grep $USER xx | cat -n

and this is part of the output:

1 USER=ciro.ruggiero
2 MAIL=/var/spool/mail/ciro.ruggiero
3 PATH=/u01/app/oracle/product/10.2.0/db_1/bin:/usr/sbin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/ciro.ruggiero/bin
4 PWD=/home/ciro.ruggiero
5 HOME=/home/ciro.ruggiero
6 LOGNAME=ciro.ruggiero

I am closing this thread. Enough information was given in several posts by three memmbers to answer the questions. Other posts have given information about where to look for a solution if you don't like the ones provided. It benefits nobody to continue with this.