The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
reading ps command's output line by line s. murat Shell Programming and Scripting 5 05-22-2008 05:23 AM
how to make a line BLINKING in output and also how to increase font size in output mail2sant Shell Programming and Scripting 3 04-14-2008 08:30 AM
How to grep / zgrep to output ONLY the matching filename and line number? vvaidyan UNIX for Dummies Questions & Answers 3 03-12-2008 05:33 PM
Multi-line output to single line LinuxRacr Shell Programming and Scripting 7 02-26-2008 10:05 AM
Making multi line output appear on one line djsal Shell Programming and Scripting 1 10-07-2004 07:21 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-15-2005
zoo591 zoo591 is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 20
Question 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
  #2 (permalink)  
Old 07-15-2005
RishiPahuja's Avatar
RishiPahuja RishiPahuja is offline
Registered User
  
 

Join Date: Apr 2005
Location: Bangalore, India
Posts: 203
Thumbs up

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


something like this should do


Code:
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

  #3 (permalink)  
Old 07-15-2005
zoo591 zoo591 is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 20
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
  #4 (permalink)  
Old 07-15-2005
RishiPahuja's Avatar
RishiPahuja RishiPahuja is offline
Registered User
  
 

Join Date: Apr 2005
Location: Bangalore, India
Posts: 203
Cool

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

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
  #5 (permalink)  
Old 07-15-2005
becket's Avatar
becket becket is offline
Registered User
  
 

Join Date: Jul 2005
Location: Liege / Belgium
Posts: 36
grep -n "$1" $SCCSPATH/*/s.*.k | cut -c1-80 | sed 's|.*/||g '
  #6 (permalink)  
Old 07-15-2005
zoo591 zoo591 is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 20
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
  #7 (permalink)  
Old 07-16-2005
futurelet futurelet is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 137

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

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:48 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0