need grep to output basename and line#

I have a script that sorta works the way I want but I would rather
just get the base name and line number from the grep output.

My current script is this one liner:

grep -n "$1" $SCCSPATH/*/s.*.k | cut -c1-80

which if I was searching for 121197 I would get something like this:

/versyss/data/devel/src/prsrc/2.20/vs220/s.195pr.k:2000:* fix 121197

I would rather get something like this from my grep output:
s.195pr.k:2000:* fix 121197

Any ideas how this can be done (non perl answers only please)

TIA
Zoo591

grep -n "$1" $SCCSPATH/*/s.*.k | cut -c1-80

something like this should do

for i in `ls -l $SCCSPATH/*/s.*.k`
do
dirn=`dirname $i`
filen=`basename $i`
cd $dirn
grep -n "$1" $filen | cut -c1-80
done

I tried your script RishiPahuja, but it had a problem with the basename
due to the -l option on ls as that supplied file attributes to the basename.

After removing the -l option your script did work it it did not show the
basename of the file beside each line#. Example of output shown below:

Looking for 121197
153:* 1.32 121197 Bug fix of gross earnings update-master and prov

1922:* fix 121197
1925:* end fix 121197
2207:* fix 121197
95:* 1.32 121197 Bug fix of gross earnings update-master and prov,

Thank you for trying.
Zoo591

Try this instead...
-l should have been -1 :stuck_out_tongue:

for i in `ls -1 $SCCSPATH/*/s.*.k`
do
dirn=`dirname $i`
filen=`basename $i`
cd $dirn
echo $filen ":" `grep -n "$1" $filen | cut -c1-80`
done

grep -n "$1" $SCCSPATH/*/s..k | cut -c1-80 | sed 's|./||g '

RishiPahuja: while your answer does provide a filename it is not quite what I am
looking for as it prints the names of file even when they do not match the
search pattern. Output of your script shown below:

s.191pr.k :
s.192pr.k :
s.194pr.k :
s.195pr.k : 153:* 1.32 121197 Bug fix of gross earnings update-master and prov 1
922:* fix 121197 1925:* end fix 121197 2207:* fix 121197

Becket: The sed add on you provided to my original script comes back with
this error:

sed: Command garbled: s|.*/||g

I appreciate your help.
Keep it coming.
Zoo591

echo '/versyss/data/devel/src/prsrc/2.20/vs220/s.195pr.k:2000:* fix 121197' | nawk -F/ '{print $NF}'

vgersh99, can you elaborate on your post please.

I tried changing my script to:

grep -n "$1" $SCCSPATH/*/s.*.k | nawk -F/ '{print $NF}'

as I thought that is where you were leading me. It ran ok in some cases
but in other cases I got garbage when I was searching for 211
Output shown below:

s.194pr.k:790:2110 nop
s.195pr.k:95:* 1.32 121197 Bug fix of gross earnings update-master <-- why the overrun?
and prov,
C" to key
s.195pr.k:1743:* fix 121197 <-- this is what I want
s.195pr.k:1746:* end fix 121197 <- and this
s.195pr.k:2000:* fix 121197
C" to key <-- but what is this?
C" to key
C" to key
C" to key
C" to key
C" to key
C" to key
C" to key
C" to key

I am not familiar with nawk, but I see the potential.

grep -n "$1" $SCCSPATH/*/s.*.k | sed -n 's%.*\([^/]*:[^:]*:.*\)%\1%p'

reborg, sorry but this wasn't what I had in mind.

Output from your script below:

:1:h21108
:1:h21174
:1:h52119
:1:h30211

that's correct

that's a correct grep as you're searching for '211'

that's also correct assuming you're searching for '211'

hm..... I don't know why you're getting this 'C" to key' strings. It might the stderr of the 'grep'. Try this:

grep -n "$1" $SCCSPATH/*/s.*.k 2> /dev/null | nawk -F/ '{print $NF}'

Yeah, sorry there was a typo in what I posted, I have updated my previous post, note the additional '*'

I tried your script with the fix. It is close to what I want, but it is not displaying the basename of the file. Output of your script shown below:

:1743:* fix 121197 <-- I would like the basename of the filename also.
:1746:* end fix 121197
:2000:* fix 121197

Close, but I can't give you the cigar.
Zoo591 :o

So far you are the closest to resolving my issue, but that redirection didn't
help. Output of your script shown below:

s.195pr.k:95:* 1.32 121197 Bug fix of gross earnings update-master <-- still getting overrun of 80 characters.
and prov,

s.195pr.k:1743:* fix 121197
s.195pr.k:1746:* end fix 121197
s.195pr.k:2000:* fix 121197
C" to key <-- still getting these
C" to key
C" to key
C" to key

Thanks for trying again.
Zoo591 :o

d'oh, yeah, forgot to tell it to include the basename, the usual problem of using too general a regular expression.

grep -n "$1" $SCCSPATH/*/s.*.k | sed -n 's%.*/\([^/]*:[^:]*:.*\)%\1%p'

I also think that the problem you may be having with the nawk solution is because the ouput of the grep for some matches contains a '/'

Reborg, this worked great! I appended a cut command to your script
I got exactly what I was looking for:

ACCEPTED ANSWER:
The final version of the script:
grep -n "$1" $SCCSPATH/*/s..k | sed -n 's%.*/\([^/]*:[^:]*:.\)%\1%p' | cut -c1-79

BTW: You are also right about the problems I was having with the nawk solution.

Thanks to all who responded.
Zoo591

just for the grins.....
grep -n "$1" $SCCSPATH/*/s.*.k | nawk -F/ 'NF > 1 {print $NF}'

I tried it but still no go.

vgersh99, I don't know if this helps but Reborg's script outputs:

s.154pl.k:240: spool (6) "211I/C" to key

where as your script chops this into:

C" to key

If you come up with another solution I will try it and let you know, but I am turning off the 'puter for the night.

Thanks again.
Zoo591

nawk '$0~x{s=FILENAME":"FNR":";sub(".*/","",s);print s $0}' x="$1" $SCCSPATH/*/s.*.k