how to make pattern search dynamic in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to make pattern search dynamic in awk
# 1  
Old 03-23-2011
how to make pattern search dynamic in awk

Hi,

I have a data in a file like below -
Code:
andy 22 abc 30000 wallstreet 
paul 30 xyz 40000 martstreet 
john 35 abc 50000 martstreet

I want to search number of employees working in a particular company. Below query executes perfectly -
Code:
awk '/abc/{ COUNT ++; }END { print "number of employees in abc=", COUNT ; }' $FILENAME

number of employees in abc=2

now i want to make pattern dynamic i.e. it should take company from users.How this can be achieved?

below piece of code that i tried is not working -
Code:
count) echo "Number of people working in each company"
echo "enter the company you want to search for"
read company
awk '/"$company"/{ COUNT ++; }END { print "number of employees in "$company"=", COUNT ; }' $FILENAME

Moderator's Comments:
Mod Comment Please use [code] and [/code] tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks.

Last edited by zaxxon; 03-23-2011 at 08:31 AM.. Reason: code tags
# 2  
Old 03-23-2011
Code:
 
awk -v comp=$comp '/'$comp'/ { C++ } END { print "No of employees in " comp " " C }' input_file
No of employees in andy 1

# 3  
Old 03-23-2011
Code:
awk '/'"$company"'/{ COUNT++; }END { print "number of employees in","'$company'",COUNT}' inputfile

# 4  
Old 03-23-2011
Thanks Panyam, it is working fine now.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk -Search pattern through Variable

Hello, We have wrote shell script for multiple file name search pattern. file format: <numner>_<20180809>.txt starting with single number and ending with 8 digits number Command: awk -v string="12_1234" -v serch="^+_+$" "BEGIN{ if (string ~/serch$/) print string }" If sting matches... (4 Replies)
Discussion started by: koti_rama
4 Replies

2. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

3. Shell Programming and Scripting

Search pattern between two quotes and make 1 row

Hi All, My file cat file " test1 test1 " " test1 test1 test1 test1" "test1 test1 test1 test1 test1 test1 "How to achieve this i want the result: cat file test1 test1 test1 test1 test1 test1 test1 test1 test1 test1 test1 test1 Please use CODE (not QUOTE) tags as required by... (4 Replies)
Discussion started by: lxdorney
4 Replies

4. Shell Programming and Scripting

awk with multiple pattern search

Want to fetch a column with multiple pattern using awk. How to achieve the same. Tried cat test address : 10.63.20.92/24 address : 10.64.22.93/24 address : 10.53.40.91/24 cat test | awk '{print $3}' |awk -F "/" '{print $1}' 10.63.20.92 10.64.22.93 10.53.40.91 Is there any... (2 Replies)
Discussion started by: Manasa Pradeep
2 Replies

5. Shell Programming and Scripting

Awk to match a pattern and perform a search after the first pattern

Hello Guyz I have been following this forum for a while and the solutions provided are super useful. I currently have a scenario where i need to search for a pattern and start searching by keeping the first pattern as a baseline ABC DEF LMN EFG HIJ LMN OPQ In the above text i need to... (8 Replies)
Discussion started by: RickCharles
8 Replies

6. Shell Programming and Scripting

awk get search pattern from a file.

Here Is a problem I am facing with awk. Query --> I want to search for a string in a file and print next 15 lines below the matched string. 1.We do not have GNU grep so cannot use grep -A or grep -B commands. 2. Instead of passing the search pattern as a string to awk. I want the awk to... (4 Replies)
Discussion started by: togotutor
4 Replies

7. UNIX for Dummies Questions & Answers

Pattern search using awk

Hi All, I am trying to find numbers with balance greater than 1 and less than equal 2 from the below file using awk Input file num ,bal 100199,1.708 100225,0 100226,0 100228,0.771166 100232,2 output file 100199,1.708 100232,2 I am using the following command for this... (2 Replies)
Discussion started by: pistachio
2 Replies

8. Shell Programming and Scripting

Pattern Search using AWK

Hi All, I have the below file data.txt.Using awk i want to grep all the zone data.Form the below command i can extact data upto of zone i give but i want it should print until next pattern. awk '/^Total Collection /{c=5;next}c-->0' zin45srs08.tools_utilization instead of c=5 is it possible... (5 Replies)
Discussion started by: ajaincv
5 Replies

9. Shell Programming and Scripting

awk search pattern

Hi, I have a log file which contains lines like below: 2010-07-19 07:13:19,021 ERROR system ...(text) 2010-07-19 07:22:03,427 ERROR system ...(text) class com... (text) 2010-07-19 07:23:19,026 ERROR system ...(text) class com... (text) each line is a separate line... I am given the a... (14 Replies)
Discussion started by: a27wang
14 Replies

10. Shell Programming and Scripting

blind search pattern with AWK

Hello, I am using a if condition within my awk script and I have a problem with the search pattern. I would like awk to find all the fields starting with 123. For now I have something like that: awk '{for(i=1;i<9;i+=2)if($i=="123"...)print $i}' test but that is not working as... (2 Replies)
Discussion started by: jolecanard
2 Replies
Login or Register to Ask a Question