Printing the continuous occurence of pattern using awk ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing the continuous occurence of pattern using awk ?
# 1  
Old 12-07-2009
MySQL Printing the continuous occurence of pattern using awk ?

Hello all,
I have a input file like this.

input file
---------------
abc ab001 + ab002 zca
acb ab006 + ab007 caz
cba ab003 + ab004 zca
bac ab004 - ab005 zac
bca ab002 - ab003 cza
cba ab005 + ab006 acz
cba ab005 + ab006 acz
cab ab007 + ab008 azc
bca ab002 + ab003 cza
cba ab003 + ab004 zca
bac ab004 + ab005 zac
acb ab006 - ab007 caz
abc ab001 - ab002 zca
cab ab007 - ab008 azc

desired output file
--------------------------
col 1 col2
---- ----
3 3
5 -2
11 6
14 -3

Explanation :-
Actually i want to print continuous "+" pattern present in column number 3 then print its occurrence number then "-" patterns occurrence number (col 2).
while (col 1) is line number in which last pattern was present.
In another way it just absolute addition (without considering sign) of values in (col 2).

Sorry for my english.
please help me out Smilie
# 2  
Old 12-07-2009
Code:
awk 'END { print NR, s c }
NR > 1 && $3 != _3 { print NR - 1, s c; c = 0 }
{ _3 = $3; c++; s = /+/ ? x : $3 }' infile

Use gawk, nawk or /usr/xpg4/bin/awk on Solaris.
# 3  
Old 12-08-2009
Hello all,

Can any one explain this code ?
Smilie
# 4  
Old 12-08-2009
awk 'END { print NR, s c } # Give the last output
NR > 1 && $3 != _3 { print NR - 1, s c; c = 0 } # When line >1, $3 is changed (compare with previous line) print the result. after print, reset count to zero.
{ _3 = $3; c++; s = /+/ ? x : $3 }' infile # reserve current $3 value, count the continuous time, reserve the - pattern status. x is nothing,empty

Change to below code, will be easy understand and readable.

Code:
 awk ' NR > 1 && $3 != temp { print NR - 1, s c; c = 0 }
{ temp = $3; c++; s = /+/ ? x : $3 } 
END { print NR, s c }'


Last edited by rdcwayx; 12-08-2009 at 03:57 PM..
# 5  
Old 12-08-2009
below perl may help you some
Code:
my $key;
while(<DATA>){
	my @tmp = split;
	if($tmp[2] ne $key){
		if($key ne ""){
			print $.-1," (",$key,")",$cnt,"\n";
			$key=$tmp[2];
			$cnt=0;
		}
		else{
			$key=$tmp[2];
		}
	}
	$cnt++;
}
print $.," (",$key,")",$cnt,"\n"; 
__DATA__
abc ab001 + ab002 zca
acb ab006 + ab007 caz
cba ab003 + ab004 zca
bac ab004 - ab005 zac
bca ab002 - ab003 cza
cba ab005 + ab006 acz
cba ab005 + ab006 acz
cab ab007 + ab008 azc
bca ab002 * ab003 cza
cba ab003 * ab004 zca
bac ab004 + ab005 zac
bca ab002 + ab003 cza
cba ab003 + ab004 zca
bac ab004 + ab005 zac
acb ab006 - ab007 caz
abc ab001 - ab002 zca
cab ab007 - ab008 azc

# 6  
Old 12-10-2009
Another one in awk:

Code:
awk '/+/ { ++p; if(n)print NR-1"-"n;n=0; } /-/ { ++n; if(p)print NR-1" "p;p=0 } 
         END{ if(p) print NR" "p+n; else print NR"-"p+n; }' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract whole word preceding a specific character pattern with first occurence of the pattern

Hello. Here is a file contents : declare -Ax NEW_FORCE_IGNORE_ARRAY=(="§" ="§" ="§" ="§" ="§" .................. ="§"Here is a pattern =I want to extract 'NEW_FORCE_IGNORE_ARRAY' which is the whole word before the first occurrence of pattern '=' Is there a better solution than mine :... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

How to get lines with only one occurence of pattern?

My data 20161220 20:30:01 MODE 1 TEST 1 SOURCE 1 SET 1 20161220 20:30:02 MODE 1 TEST 2 SOURCE 1 SET 1 20161220 20:30:02 MODE 1 TEST 3 SOURCE 1 SET 1 20161220 20:30:02 MODE 1 TEST 1 SOURCE 2 SET 1 20161220 20:30:04 MODE 1 TEST 1 SOURCE 1 SET 1 MODE 1 TEST 2 SOURCE 2 SET 1 20161220 20:30:02... (7 Replies)
Discussion started by: migurus
7 Replies

3. Shell Programming and Scripting

UNIX awk pattern matching and printing lines

I have the below plain text file where i have some result, in order to mail that result in html table format I have written the below script and its working well. cat result.txt Page 2015-01-01 2000 Colors 2015-02-01 3000 Landing 2015-03-02 4000 #!/bin/sh LOG=/tmp/maillog.txt... (1 Reply)
Discussion started by: close2jay
1 Replies

4. Shell Programming and Scripting

Help! Printing out CSV output from awk Pattern Match

Hi, I need to search for a word using Awk and print out the line the word is in and every line after the search phrase until I hit this #------------. Then I need to send it to a csv file. So basically the input file format is like this:... (1 Reply)
Discussion started by: An0mander
1 Replies

5. Shell Programming and Scripting

awk pattern match not printing desired columns

Hi all, I'm trying to match the following two files with the code below: awk -F, 'NR==FNR {a=$0; next} ($12,$4) in a {print $12,$1,a}' OFS="," file4.csv file3.csv but the code does not print the entire row from file4 in addition to column 12 and 1 of file3. file4: o,c,q,co,ov,b... (1 Reply)
Discussion started by: bkane3
1 Replies

6. Shell Programming and Scripting

Match pattern and print the line number of occurence using awk

Hi, I have a simple problem but i guess stupid enough to figure it out. i have thousands rows of data. and i need to find match patterns of two columns and print the number of rows. for example: inputfile abd abp 123 abc abc 325 ndc ndc 451 mjk lkj... (3 Replies)
Discussion started by: redse171
3 Replies

7. Shell Programming and Scripting

Help with using awk to print pattern/occurence

Hi, Do anybody know how to use awk to count the pattern at specific column? Input file M2A928K 419 ath-miR159a,gma-miR159a-3p,ptc-miR159a 60 miR235a . . Output file M2A928K 419 ath-miR159a,gma-miR159a-3p,ptc-miR159a 60 miR235a 3 . . I plan to count how many "miR" in column 3... (2 Replies)
Discussion started by: cpp_beginner
2 Replies

8. Shell Programming and Scripting

awk seach and printing a particular pattern

Hi i need a help for this. the output of a command is like below 1 /tmp/x 2.2K /tmp/y 3.2k /tmp/z 1G /tmp/a/b 2.2G /tmp/c 3.4k /tmp/d Now i need to grep for the paths which are in GB..like below 1G /tmp/a/b 2.2G /tmp/c pls suggest me, how can i... (12 Replies)
Discussion started by: kumar85shiv
12 Replies

9. Shell Programming and Scripting

Searching and printing the only pattern using awk,sed or perl

Hi All, i have an output of command vmstat as below : $ vmstat System configuration: lcpu=4 mem=5376MB ent=1.00 kthr memory page faults cpu ----- ----------- ------------------------ ------------ ----------------------- r b avm fre re pi... (10 Replies)
Discussion started by: omkar.jadhav
10 Replies

10. UNIX for Dummies Questions & Answers

Pattern occurence in a file

How can we find the number of occurence of a specified pattern in a file? This command would be a part of a shell script. (5 Replies)
Discussion started by: videsh77
5 Replies
Login or Register to Ask a Question