String search - Command to find second occurance | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

String search - Command to find second occurance

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-06-2007
saurabhsinha23 saurabhsinha23 is offline
Registered User
 
Join Date: Dec 2007
Last Activity: 19 February 2008, 12:48 PM EST
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
String search - Command to find second occurance

Hi,

I am new to Unix world. Is there any command which can directly return the second occurance of a particular string in a file? Basically, I want to read the contents of the file from the second occurance of a particualr string. Can be implemented using a loop, but am just wondering if there is a command which can help me do so.

Please let me know.

TIA
Saurabh
Sponsored Links
    #2  
Old 12-06-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 31 July 2014, 11:11 AM EDT
Location: NM
Posts: 10,160
Thanks: 269
Thanked 781 Times in 731 Posts
You can use awk something like this -

Code:
awk '{
          if(index($0, "some string")> 0) {count++}
          if(count>1) { print $0}
        } ' inputfile > outputofsearch

Sponsored Links
    #3  
Old 12-06-2007
saurabhsinha23 saurabhsinha23 is offline
Registered User
 
Join Date: Dec 2007
Last Activity: 19 February 2008, 12:48 PM EST
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for your prompt reply. I am not familiar with awk. Is there any alternative method you can think of..
    #4  
Old 12-06-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 31 July 2014, 11:11 AM EDT
Location: NM
Posts: 10,160
Thanks: 269
Thanked 781 Times in 731 Posts
Other than looping or using awk, perl, or python, I have no other idea except an extreme hack like this:


Code:
#!/bin/ksh

# total line in file
wc -l inputfile | read filelen dummy

# find the line to start reading on
grep -n 'some string' inputfile | head -n 2 | tail -n 1 | read startline dummy

# subtract start line from total 
length=$(( $filelen - $startline ))

# tail the lines you need.
tail -n $length inputfile

If you are running this against a large file it will waste considerable resources.
It wastes resources anyway, because it reads thru the file several times.
Sponsored Links
    #5  
Old 12-06-2007
saurabhsinha23 saurabhsinha23 is offline
Registered User
 
Join Date: Dec 2007
Last Activity: 19 February 2008, 12:48 PM EST
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Bug

Thanks Jim. This really helps. Let me try it.
Sponsored Links
    #6  
Old 12-06-2007
drl's Avatar
drl drl is offline Forum Advisor  
Registered Voter
 
Join Date: Apr 2007
Last Activity: 30 July 2014, 8:39 AM EDT
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 1,663
Thanks: 34
Thanked 186 Times in 170 Posts
Hi.

Command csplit can help:

Code:
#!/usr/bin/env sh

# @(#) s1       Demonstrate splitting a file at second occurrence of string.

set -o nounset
echo

debug=":"
debug="echo"

## Use local command version for the commands in this demonstration.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash csplit

echo

# Remove debris from previous runs.
rm -f xx*

FILE=${1-data1}

echo
echo " Input file:"
cat -n $FILE

echo
echo " Character counts written to files:"
csplit -k -z $FILE /saurabhsinha23/ {1}

echo
echo " Files created by csplit:"
ls xx*

echo
echo " Results from csplit:"
cat xx02

exit 0

Producing:

Code:
% ./s1

(Versions displayed with local utility "version")
GNU bash 2.05b.0
csplit (coreutils) 5.2.1


 Input file:
     1  First line
     2  many lines can be in the zeroth segment
     3  vanilla lines
     4  this line contains "saurabhsinha23"
     5  another line
     6  yet another line
     7  an additional line
     8  second occurrence of "saurabhsinha23"
     9  more lines
    10  many more lines
    11  third occurrence of "saurabhsinha23"
    12  lines continue
    13  fourth occurrence of "saurabhsinha23"
    14  ...
    15  last occurrence of "saurabhsinha23"
    16  last line

 Character counts written to files:
65
85
205

 Files created by csplit:
xx00  xx01  xx02

 Results from csplit:
second occurrence of "saurabhsinha23"
more lines
many more lines
third occurrence of "saurabhsinha23"
lines continue
fourth occurrence of "saurabhsinha23"
...
last occurrence of "saurabhsinha23"
last line

See man pages for details ... cheers, drl
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Search and replace only the first occurance suraj.sheikh Shell Programming and Scripting 3 10-13-2011 04:34 AM
To find the Nth Occurence of Search String mac4rfree UNIX for Dummies Questions & Answers 3 07-27-2011 02:16 AM
Awk - find string, search lines below string for other strings Mbohmer Shell Programming and Scripting 6 03-29-2011 05:22 PM
search for a word and it's occurance at the output legendofanatoli UNIX for Dummies Questions & Answers 2 05-05-2008 04:52 PM
Unix find command to print directory and search string princein UNIX for Dummies Questions & Answers 4 03-06-2007 07:46 AM



All times are GMT -4. The time now is 01:35 PM.