need grep to output basename and line#


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need grep to output basename and line#
# 15  
Old 07-15-2005
Quote:
Originally Posted by zoo591
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:
d'oh, yeah, forgot to tell it to include the basename, the usual problem of using too general a regular expression.

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

Last edited by reborg; 07-15-2005 at 09:11 PM..
# 16  
Old 07-15-2005
MySQL

Quote:
Originally Posted by reborg
d'oh, yeah, forgot to tell it to include the basename, the usual problem of using too general a regular expression.

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

Last edited by zoo591; 07-15-2005 at 10:09 PM..
# 17  
Old 07-15-2005
just for the grins.....
grep -n "$1" $SCCSPATH/*/s.*.k | nawk -F/ 'NF > 1 {print $NF}'
# 18  
Old 07-15-2005
Quote:
Originally Posted by vgersh99
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

Last edited by zoo591; 07-16-2005 at 06:57 PM..
# 19  
Old 07-16-2005
Code:
nawk '$0~x{s=FILENAME":"FNR":";sub(".*/","",s);print s $0}' x="$1" $SCCSPATH/*/s.*.k

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

How to control grep output intact for each matching line?

I have multiple (~80) files (some can be as big as 30GB of >1 billion of lines!) to grep on a pattern, and piped the match to a single file. I have a 96-core machine so that each grep job was sent to the background to speed up the search: file1.tab chr1A_part1 123241847 123241848... (6 Replies)
Discussion started by: yifangt
6 Replies

3. Shell Programming and Scripting

Grep echo awk print all output on one line

Hello, I've been trying to find the answer to this with Google and trying to browse the forums, but I haven't been able to come up with anything. If this has already been answered, please link me to the thread as I can't find it. I've been asked to write a script that pulls a list of our CPE... (51 Replies)
Discussion started by: rwalker
51 Replies

4. UNIX for Dummies Questions & Answers

how to grep a number from output line

I`m having a output shown below, CFR 235,BBC DM-2 ALL CFR 111,BBC DM-2 ALL CFR 333,BBC DM-2 ALL from the above Output i want to use 235,111,333 as input for other purpose. these no always change every time i run script.so please suggest me the way i could do it with example,i have tried... (5 Replies)
Discussion started by: nitin_aaa27
5 Replies

5. Shell Programming and Scripting

Remove a specific line from grep output string

Dear All I want to search string "1000" from input file and if it found i want remove line that contain 1000 and also remove 3 line above it and 2 line below it. INPUT FILE: BHAT-D 2 aaa ID CODE GS UPDATE MODE LANG MCO MCL NUMPAGES 50 ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

6. Shell Programming and Scripting

Grep multiple line pattern and output the lines

Hi I have the following Input -- -- TABLE: BUSINESS_UNIT -- ALTER TABLE RATINGS.BUSINESS_UNIT ADD CONSTRAINT FK1_BUSINESS_UNIT FOREIGN KEY (PEOPLESOFT_CHART_FIELD_VALUE_ID) REFERENCES RATINGS.PEOPLESOFT_CHART_FIELD_VALUE(PEOPLESOFT_CHART_FIELD_VALUE_ID) ; ALTER TABLE... (1 Reply)
Discussion started by: pukars4u
1 Replies

7. Solaris

Send cut output to basename

Hi, I'm running a command : pargs 20392 | egrep -e "-f "|cut -d " " -f3 | basename BUT the o/p of cut is not sending to basename. the o/p of: pargs 20392 | egrep -e "-f "|cut -d " " -f3 is /home/staff/Properties.cfg Appreciated ur help.. (2 Replies)
Discussion started by: axes
2 Replies

8. UNIX for Dummies Questions & Answers

problem with output of find command being input to basename command...

Hi, I am triying to make sure that there exists only one file with the pattern abc* in path /path/. This directory is having many huge files. If there is only one file then I have to take its complete name only to use furter in my script. I am planning to do like this: if ; then... (2 Replies)
Discussion started by: new_learner
2 Replies

9. UNIX for Dummies Questions & Answers

Grep or other ways to output line above and/or below searched line

Hi all, Would like to know how I could search for a string 'xyz' but have the output show the line plus the line above and/or below all lines found. eg. search for xyz from file containing: abc 12345 asdf xyz asdfds wwwww kjkjkj ppppp kkkxyz eeee zzzzz and the output to... (2 Replies)
Discussion started by: sammac
2 Replies

10. UNIX for Dummies Questions & Answers

How to grep / zgrep to output ONLY the matching filename and line number?

Hi all, I am trying to zgrep / grep list of files so that it displays only the matching filename:line number and does not display the whole line, like: (echo "1.txt";echo "2.txt") | xargs zgrep -no STRING If I use -o option, it displays the matching STRING and if not used, displays the... (3 Replies)
Discussion started by: vvaidyan
3 Replies
Login or Register to Ask a Question