find/grep two items and display both


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users find/grep two items and display both
# 1  
Old 06-13-2012
find/grep two items and display both

I have a file against which I can grep a string for. I can also check for that string count using wc -l (or grep -c). I need to display the results of both in one output i.e. 'line containing string' and 'count' - what would be the most efficient way of managing this? Thanks in advance.
# 2  
Old 06-14-2012
Try...
Code:
awk '/string/{print "line=" NR, "count=" ++cnt}' file1

# 3  
Old 06-14-2012
Please post what Operating System and version you are running and what Shell you use.

Assuming some sort of modern Bourne-type Shell:

Not particularly efficient (because it reads the input file three times), but using commands mentioned in post #1.
Code:
filename="/path/name_of_file"
string="search string"
found=`grep -l "${string}" "${filename}"`
if [ ! "${found}""X" = "X" ]
then
          grep "${string}" "${filename}"
          count=`grep -c "${string}" "${filename}"`
          echo "count = ${count}"
fi


There is a better solutions using a workfile to hold the hits and then counting the hits.

Last edited by methyl; 06-19-2012 at 11:38 AM.. Reason: noticed mispaste
# 4  
Old 06-19-2012
thank you to both forum members who responded - much appreciated. I will try the solution today.

---------- Post updated at 09:55 AM ---------- Previous update was at 09:54 AM ----------

Also, I need to be able to use it on either Red Hat/Ubuntu (I can use any shell provided the solution works). Once again, thank you for looking into this.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read a lis, find items in a file from the list, change each item

Hello, I have some tab delimited text data, file: final_temp1 aname val NAME;r'(1,) 3.28584 r'(2,)<tab> NAME;r'(3,) 6.13003 NAME;r'(4,) 4.18037 r'(5,)<tab> You can see that the data is incomplete in some cases. There is a trailing tab after the first column for each incomplete row. I... (2 Replies)
Discussion started by: LMHmedchem
2 Replies

2. Shell Programming and Scripting

Find odd numbered items from a csv

Hi, This question might seem bit confusing. Sorry for that. I have tried to express myself as clear as possible. I have a text file in following format: abc_456_def,abc_457_def,123_458_def,abc_3_459_def,zbc_123_456_def ybc_123_457_def,xbc_3_458def,ybc_124_457_def I want to check the... (3 Replies)
Discussion started by: ctrld
3 Replies

3. UNIX for Dummies Questions & Answers

To find similar items in a column

HI, I have a long file which looks like "1xxx_0_1" "1xxx" 500 5 "ABC*3-DEF*3-LL" "2yyy_0_1" "2yyy" 600 10 "ABC*2-DEF*2-LL" "3ddd_0_1" "3ddd" 150 52 "ABC*3-DEF*3-LL" "1xxx_0_1" "1xxx" 500 5 "ABC*3-DEF*3-LL" "2yyy_0_1" "2yyy" 600 10 "ABC*2-DEF*2-LL" ... (3 Replies)
Discussion started by: XXLMMN
3 Replies

4. Shell Programming and Scripting

Need help with a script to grep items in one file from another file

I have one master file "File1" with all such info in it. I need to grep each object under each list from another file "File2". Can anyone help me with a script for this. File 1 ------ List 1 Object 1 Object 2 List 2 Object 3 Object 1 List 3 Object 2 ... (5 Replies)
Discussion started by: Sam R
5 Replies

5. UNIX for Dummies Questions & Answers

Grep - Searching for multiple items using one command

I am performing a regular check on UNIX servers which involves logging onto UNIX servers and using the grep command to check if a GID exists in the /etc/group directory e.g. grep 12345 /etc/group I have five to check on each server, is there anyway I can incorporate them into one command and... (2 Replies)
Discussion started by: @MeDaveT
2 Replies

6. Shell Programming and Scripting

awk between items including items

OS=HP-UX ksh The following works, except I want to include the <start> and <end> in the output. awk -F '<start>' 'BEGIN{RS="<end>"; OFS="\n"; ORS=""} {print $2} somefile.log' The following work in bash but not in ksh sed -n '/^<start>/,/^<end>/{/LABEL$/!p}' somefile.log (4 Replies)
Discussion started by: Ikon
4 Replies

7. Shell Programming and Scripting

Make grep -c display like grep -n?

Hey Guys, Wondering if there is a way to do the following I have a file called test.txt abc def abc abc def I have a pattern file called pattern.txt containing the following abc def I want to do a count, but have it display the count value preceeding each line like grep -n (2 Replies)
Discussion started by: Jerrad
2 Replies

8. UNIX for Dummies Questions & Answers

Grep and display

I have the following data: A 1 2 3 A 4 5 6 A 7 8 9 I want to grep this data with A and 3 lines below it then display them in this format: A 1 2 3 A 4 5 6 A 7 8 9 (4 Replies)
Discussion started by: bobo
4 Replies
Login or Register to Ask a Question