LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 11-12-2012, 11:47 AM   #1
puntino
Member
 
Registered: Sep 2006
Location: Italy
Distribution: Suse 10.1
Posts: 71

Rep: Reputation: 15
How to find a specific string sequence in files


Hello,
I'd like to know how to find a specific string sequence in files.
For instance, suppose that the file1 holds a sequence like the following
...
ext3_write_super
ext3_ordered_writepage
journal_blocks_per_page
journal_start
kmem_cache_alloc
block_write_full_page
generic_make_request
journal_dirty_data
journal_stop
ext3_write_inode
ext3_bmap
generic_block_bmap
ext3_write_super
ext3_ordered_writepage
journal_blocks_per_page
kmem_cache_alloc
...

I would like to know the line number and count all the sequence like:
ext3_ordered_writepage
journal_blocks_per_page
journal_start
kmem_cache_alloc


the sequence must be matched exactly in file1. Wrt to file 1, the sequence
occurs twice.

I've tried with
Code:
grep -nFf sequence_to_find file1
but it's not what I'm looking for.
in "sequence_to_find" there are the former sequences.
I'd like to have an output like:
count 2
2 ext3_ordered_writepage
3 journal_blocks_per_page
4 journal_start
5 kmem_cache_alloc
13 ext3_ordered_writepage
14 journal_blocks_per_page
15 journal_start
16 kmem_cache_alloc

Is there any way to do that?
Thank you in advance
 
Old 11-12-2012, 01:51 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,307

Rep: Reputation: 767Reputation: 767Reputation: 767Reputation: 767Reputation: 767Reputation: 767Reputation: 767
Quote:
Originally Posted by puntino View Post
I've tried with
Code:
grep -nFf sequence_to_find file1
but it's not what I'm looking for.
What is wrong with that output? Is it just the colons you'd like removed?
Code:
grep -nFf sequence_to_find file1 | tr : ' '
 
Old 11-12-2012, 03:16 PM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,362

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
Hi ntubski! I think the problem is that the -f option causes grep to match every single line in the pattern file, not the exact sequence as a whole.

@puntino: here is a tricky solution in awk:
Code:
BEGIN {

  RS = "ext3_ordered_writepage\njournal_blocks_per_page\njournal_start\nkmem_cache_alloc"
  
  split(RS,pattern,"\n")
  
}

{

  _[++c] = n += gsub(/\n/,"")

}

END {

  print "count", NR - 1
  
  for ( i = 1; i < c; i++ ) {
    start = _[i] +1 -(i-1) +4*(i-1)
    print start, pattern[1]
    print start+1, pattern[2]
    print start+2, pattern[3]
    print start+3, pattern[4]
  }
  
}
I think there are more elegant solutions using perl, python or ruby, but I prefer to leave them to the experts of these languages!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] find, print and count specific characters after string criteria Alkass Linux - Newbie 2 05-03-2011 09:36 AM
Searching a specific directory for files containing a text string lothwen Linux - Newbie 1 11-20-2010 10:58 AM
How to find a file with specific string inside it. pinga123 Linux - Newbie 5 05-07-2010 06:30 AM
Remove files that contain a specific string poymode Linux - General 5 02-17-2010 03:01 AM
How to find those files having specific string??? saeed Red Hat 4 07-19-2006 11:50 AM


All times are GMT -5. The time now is 10:02 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration