Getting number of lines of nth occurrency


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Getting number of lines of nth occurrency
# 1  
Old 12-09-2009
Getting number of lines of nth occurrency

Hi all,
I would like to extract the line number of the n-th occurrency of a given string in a file.
e.g.

xxx
yyy
xxx
zzz
xxx

the second occurrency of xxx is at line 3.

What is the fastest way to do it in bash?
Thank you,
# 2  
Old 12-09-2009
Try:

Code:
awk '/xxx/ && (++x==2){ print NR}'  file

# 3  
Old 12-09-2009
Hi Dennis,

i think 5 must be replaced by 2 .


awk '/xxx/ && (++x==2){ print NR}' filename

Thanks,
Penchal
# 4  
Old 12-09-2009
Thanks,
that works.
what if xxx and 2 are variables and I want to assign the value to a variable?

I tried
Code:
STRING=xxx
ID=2

NUMBER=`awk '/$STRING/ && (++x==$ID){ print NR}' file`
echo $NUMBER

but it doesn't work
# 5  
Old 12-09-2009
A bit prettier using variables:
Code:
$ awk -vSTR=xxx -vNUM=2 '$0 ~ STR { occ++ } occ == NUM {print NR; exit }' test.txt
3
$ awk -vSTR=xxx -vNUM=3 '$0 ~ STR { occ++ } occ == NUM {print NR; exit }' test.txt
5

That way you won't have to play around with double and single quotes. Just replace xxx and the number by any variable you like.
# 6  
Old 12-09-2009
Try:

Code:
var1=xxx
var2=2
awk "/$var1/ && (++x==$var2){ print NR}"  file

# 7  
Old 12-09-2009
Thank you.
I have the problem that also the file name is variable

Code:
var1=xxx
var2=2
file=file_name
NUMBER=`awk "/$var1/ && (++x==$var2){ print NR}"  $file`
echo NUMBER

will give
Code:
23199 file_name

so filename becomes part of number!
How to solve it?

Last edited by f_o_555; 12-09-2009 at 05:34 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Getting the lines with nth column non-null

Hi, I have a huge list of archives (.gz). Each archive is about 40MB. A file is generated every minute so if I want to analyze the data for 1 hour I get already 60 files for example. These are text files, ';' separated, each line having about 300 fields (columns). What I need to do is to... (11 Replies)
Discussion started by: Nenad
11 Replies

2. Shell Programming and Scripting

Pairing the nth elements on multiple lines iteratively

Hello, I'm trying to create a word translation section of a book. Each entry in the word list will come from a set of linguistically analyzed texts. Each sentence in the text has the following format. The first element in each line is the "name" of the line (i.e. "A","B","C","D"). The... (12 Replies)
Discussion started by: John Lyon
12 Replies

3. Shell Programming and Scripting

Using paste command every nth number of file

Hi, I want to use paste command in a loop that does it every 6 files. My sample files are like the ones below. 20010101.txt 20010106.txt 20010111.txt 20010116.txt 20010121.txt 20010126.txt 20010131.txt 20010205.txt 20010210.txt 20010215.txt 20010220.txt 20010225.txt 20010302.txt... (4 Replies)
Discussion started by: ida1215
4 Replies

4. Emergency UNIX and Linux Support

Replace nth position character of all the lines in file

I want to replace 150th character of all the lines in a file using sed or awk... searched the forums but didn't find exact answer (9 Replies)
Discussion started by: greenworld123
9 Replies

5. Shell Programming and Scripting

Extracting lines after nth LINE from an output

Hi all, Here is my problem for which i am breaking my head for past three days.. I have parted command output as follows.. Model: ATA WDC WD5000AAKS-0 (scsi) Disk /dev/sdb: 500GB Sector size (logical/physical): 512B/512B Partition Table: msdos Number Start End Size Type ... (3 Replies)
Discussion started by: selvarajvs
3 Replies

6. Shell Programming and Scripting

How to output all lines following Nth occurrence of string

Greetings experts. Searched the forums (perhaps not hard enough?) - Am searching for a method to capture all output from a log file following the nth occurrence of a known string. Background: Using bash, I want to monitor my Oracle DB alert log file. The script will count the total # of... (2 Replies)
Discussion started by: cjtravis
2 Replies

7. Shell Programming and Scripting

find string nth occurrence in file and print line number

Hi I have requirement to find nth occurrence in a file and capture data from with in lines (between lines) Data in File. <QUOTE> <SESSION> <ATTRIBUTE NAME='Parameter Filename' VALUE='file1.parm'/> <ATTRIBUTE NAME='Service Name' VALUE='None'/> </SESSION> <SESSION> <ATTRIBUTE... (6 Replies)
Discussion started by: tmalik79
6 Replies

8. Shell Programming and Scripting

Print lines with specific character at nth position in a file

I need to print lines with character S at nth position in a file...can someone pl help me with appropriate awk command for this (1 Reply)
Discussion started by: manaswinig
1 Replies

9. Shell Programming and Scripting

Print lines with specific character at nth position in a file

I need to print lines with character S at nth position in a file...can someone pl help me with appropriate awk command for this (2 Replies)
Discussion started by: manaswinig
2 Replies

10. UNIX for Dummies Questions & Answers

Grepping nth line number

How do you grep every nth line number from a file? (2 Replies)
Discussion started by: shabs1985
2 Replies
Login or Register to Ask a Question