|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
Thanks for your prompt reply. I am not familiar with awk. Is there any alternative method you can think of..
|
|
#4
|
|||
|
|||
|
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
|
|||
|
|||
|
Thanks Jim. This really helps.
Let me try it. |
| Sponsored Links | |
|
|
#6
|
||||
|
||||
|
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 0Producing: 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 lineSee man pages for details ... cheers, drl |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|