awk statement to grep (bit involved)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk statement to grep (bit involved)
# 1  
Old 05-14-2014
awk statement to grep (bit involved)

Hellow:
I have the following data.
Code:
id1 xxx xxx xxx  id1 xxxx xxx xxx
id2 xxx xxx xxx  id2 xxxx xxx xxx id2

In my awk script which reads the file containing the above data I have the following code:
Code:
myline=<inputdata> which is shown above

What I am trying to find out is how may occurrences of id1 are there in the above line. So the expected out put is:

Code:
id1 no of occurrences: 2
id2 no of occurrences :3

Given a line assigned to a variable, I want to know how many particular tokens are there in that variable. Please let me know.
# 2  
Old 05-14-2014
try
Code:
nawk '{ if (gsub("id1", "&1", $0) > 0) {print "id1 no of occurrences: " gsub("id1", "&1", $0)}}' filename &&
nawk '{ if (gsub("id2", "&1", $0) > 0) {print "id2 no of occurrences: " gsub("id2", "&1", $0)}}' filename

# 3  
Old 05-14-2014
Code:
$ echo a b c a b c a b c | awk ' { L=$0 ; while(sub("(^| )"MATCH"( |$)", "", L)) N++;  print N }' MATCH="a"

3

$

These 2 Users Gave Thanks to Corona688 For This Post:
# 4  
Old 05-14-2014
Code:
awk '{for(i=1;i<=NF;i++) ($i==$1)?a[$1]++:a[$1]}END{for(i in a) print i " no of occurrences: " a[i]}' myFile

---------- Post updated at 12:24 PM ---------- Previous update was at 12:11 PM ----------

Quote:
Originally Posted by Corona688
Code:
$ echo a b c a b c a b c | awk ' { L=$0 ; while(sub("(^| )"MATCH"( |$)", "", L)) N++;  print N }' MATCH="a"

3

$

or a variation on the same theme:
Code:
awk '{ a[$1]+=gsub("(^| )"$1"( |$)", "&", $0)} END {for (i in a) print i " no of occurrences: " a[i]}' myFile


Last edited by vgersh99; 05-14-2014 at 01:20 PM..
# 5  
Old 07-15-2014
Quote:
Originally Posted by Corona688
Code:
$ echo a b c a b c a b c | awk ' { L=$0 ; while(sub("(^| )"MATCH"( |$)", "", L)) N++;  print N }' MATCH="a"

3

$

My string looks like this:
Code:
echo 20140403 07:26:26 IN:CTRL=[CIN=CIN3]:TYP=TYP2:DCN=DCN3:DATA=[xxxxx DCN3 xxxxxxxxx xxxx DCN3 xxx xxx xx xxxx xxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  xxxx xxxDCN3   xxxxxxxxxxx  xxxxxx  xxxxx]|awk ' { L=$0 ; while(sub("(^|
)"MATCH"( |$)", "", L)) N++;  print N }' MATCH="DCN3"

I am expecting 4 occurrences of DCN3. but getting only 2 as output. Please let me know how to get the exact occurrences.
# 6  
Old 07-15-2014
This: xxxDCN3 is not being matched, do you want to match your string, even when is part of a longer word?

If so try:

Code:
awk '{N+=gsub(MATCH, "&")} END{print N}' MATCH="DCN3"

Or if you only have 1 line of input:

Code:
awk '$0=gsub(MATCH, x)' MATCH="DCN3"


Last edited by Chubler_XL; 07-15-2014 at 05:20 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

2. Shell Programming and Scripting

Using GREP in IF Statement

Hello All, I have 2 different pieces of code, I am confused why the Code1 is giving me the correct result where as the Code2 is not giving me correct result. It gives me always result as "Failure" irrespective of the "ERROR" word exists in logfile or not. may I know the reason why? I am using Bash... (17 Replies)
Discussion started by: Ariean
17 Replies

3. Shell Programming and Scripting

Use awk/sed/grep with goto statement!

Hi, I have an array with characters and I am looking for specific character in that array and if those specific character not found than I use goto statment which is define somehwhere in the script. My code is: set a = (A B C D E F) @ i = 0 while ($i <= ${#a}) if ($a != "F" || $a != "D")... (3 Replies)
Discussion started by: dixits
3 Replies

4. Shell Programming and Scripting

if statement with grep as conditional

Please see the script segment below for i in $files do echo $i if ; then case "$1" in "IE0263"|"IE0264"|"IE0267"|"IE0268") short_filename=`ls -l $i | cut -c108-136 | sort` ;; "IE0272"|"IE0273") short_filename=`ls -l $i | cut... (4 Replies)
Discussion started by: jmahal
4 Replies

5. Shell Programming and Scripting

Grep statement

Hi All, Please can somebody advise that if I want to search a pattern xyz the grep command should only select xyz and not any other pattern containing xyz (ex abxyzcd) Regards (1 Reply)
Discussion started by: Shazin
1 Replies

6. Shell Programming and Scripting

Using grep in a test/if statement

Okay, well this is more or less my first attempt at writing a shell script. Anyways, here's my code: cd ${PATH} if then rm ${FILE} ./anotherScript else exit 1 fi exit 1 Anyways, it's a pretty simple script that is supposed to search for the... (4 Replies)
Discussion started by: cbo0485
4 Replies

7. UNIX for Advanced & Expert Users

grep high bit char

Hi - I have file which contains high bit unicode chars like © etc.. How can I do grep to find out lines which contain copyright symbol © I tried using grep \x{00A9} grep \x\{00A9\} Thanks- (7 Replies)
Discussion started by: ayyo1234
7 Replies

8. Shell Programming and Scripting

Using grep in if statement

Can somebody please guide me towards right syntax: #!/bin/ksh if i = $(grep $NAME filename) echo "Name Found" else echo " Name not Found" fi I need to grep for $NAME in the file, and if it returns false, execute a series of commands and if true, exit out. The above is not the right... (3 Replies)
Discussion started by: chiru_h
3 Replies

9. HP-UX

How to check patches involved

Hi I need to check if the following patches are installed in a HP-UX machine "GOLDQPK11i, which in turn includes both GOLDAPPS11i and GLODBASE11i How can I go about doing it, am still a struggling sys admin! Saw this command showrev -p but command not found in the machine thou! Thanks... (3 Replies)
Discussion started by: gelbvonn
3 Replies

10. UNIX for Dummies Questions & Answers

Help! a free Sprite is involved!

Can anyone define the following for me? /etc/rc.c/init.d/iptables restart Our resident "Geek" is giving away a prize if I can tell him what this means. (5 Replies)
Discussion started by: txyzzy
5 Replies
Login or Register to Ask a Question