Line Number Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Line Number Script
# 1  
Old 12-01-2009
Line Number Script

If i have text file how do i find the line with the nth occurance of a certain string, so in example below i want line number of 3rd occurance of COD1 which should be line 7

COD1
COD2
COD2
COD1
COD2
COD2
COD1
# 2  
Old 12-01-2009
Debian

Code:
awk '/COD1/&&++c==3&&$0=NR' infile

or:

Code:
awk '/COD1/&&++c==3{print NR}' infile

# 3  
Old 12-01-2009
How do i run this in a script and assing it to a variable.

I try something like:

numVal=`awk '/COD1/&&++c==3{print NR}' infile`

And get the following error when running script

numVal : command not found
# 4  
Old 12-01-2009
Code:
$ awk -v numVal=3 '/COD1/&&++c==numVal {print NR}' urfile
7

$ awk -v numVal=2 '/COD1/&&++c==numVal {print NR}' urfile
4

# 5  
Old 12-01-2009
This might also be interesting in some cases:
Code:
grep -nm3 COD1 infile

# 6  
Old 12-01-2009
shell
Code:
i=0
linenum=0
while read -r line
do
    linenum=$((linenum+1))
    case "$line" in
    *COD2* )
        i=$((i+1))
        [ "$i" -eq 3 ] && echo $linenum && exit        
    ;;
    esac
done <"file"

# 7  
Old 12-01-2009
Perl:

Code:
$
$ cat -n f6
     1  COD1
     2  COD2
     3  COD2
     4  COD1
     5  COD2
     6  COD2
     7  COD1
$
$ perl -lne 'if (/COD1/){$i++; print $. if $i == 3}' f6
7
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get the line number in shell script

I have one text file 1 2 3 a 5 4 4 3 where i want to print the line number while read line do line_no=`awk '{print NR, $0}'` echo 'In line no $line_no' done <$txt_file If i run the above code, it will print 'In line no 1 1 2 3' It prints the line number with the whole... (3 Replies)
Discussion started by: RJG
3 Replies

2. Shell Programming and Scripting

Write $line number into textfile and read from line number

Hello everyone, I don't really know anything about scripting, but I have to manage to make this script, out of necessity. #!/bin/bash while read -r line; do #I'm reading from a big wordlist instructions using $line done Is there a way to automatically write the $line number the script... (4 Replies)
Discussion started by: bobylapointe
4 Replies

3. Shell Programming and Scripting

sed script - print the line number along with the line

Hi, I want to print the line number with the pattern of the line on a same line using multi-patterns in sed. But i don't know how to do it. For example, I have a file abc def ghi I want to print 1 abc 2 def 3 ghi I know how to write it one line code, but i don't know how to put... (11 Replies)
Discussion started by: ntpntp
11 Replies

4. Shell Programming and Scripting

Shell script to count number of ~ from each line and compare with next line

Hi, I have created one shell script in which it will count number of "~" tilda charactors from each line of the file.But the problem is that i need to count each line count individually, that means. if line one contains 14 "~"s and line two contains 15 "~"s then it should give an error msg.each... (3 Replies)
Discussion started by: Ganesh Khandare
3 Replies

5. UNIX for Dummies Questions & Answers

How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone. I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies

6. Shell Programming and Scripting

how to get the data from line number 1 to line number 100 of a file

Hi Everybody, I am trying to write a script that will get some perticuler data from a file and redirect to a file. My Question is, I have a Very huge file,In that file I have my required data is started from 25th line and it will ends in 100th line. I know the line numbers, I need to get all... (9 Replies)
Discussion started by: Anji
9 Replies

7. UNIX for Dummies Questions & Answers

echo a line number when script is executing

BTW, this is my first post and I'm fairly new to Unix. :D I'm having issues with a korn shell script running abnormally long. What we would like to do is echo the line number the job is executing at the time. Is there any sort of function or environment variable that allows such a thing? ... (1 Reply)
Discussion started by: gavineq
1 Replies

8. Shell Programming and Scripting

Adding a columnfrom a specifit line number to a specific line number

Hi, I have a huge file & I want to add a specific text in column. But I want to add this text from a specific line number to a specific line number & another text in to another range of line numbers. To be more specific: lets say my file has 1000 lines & 4 Columns. I want to add text "Hello"... (2 Replies)
Discussion started by: Ezy
2 Replies

9. Shell Programming and Scripting

Appending line number to each line and getting total number of lines

Hello, I need help in appending the line number of each line to the file and also to get the total number of lines. Can somebody please help me. I have a file say: abc def ccc ddd ffff The output should be: Instance1=abc Instance2=def Instance3=ccc Instance4=ddd Instance5=ffff ... (2 Replies)
Discussion started by: chiru_h
2 Replies

10. Shell Programming and Scripting

Unix Script with line number at beginning of each line.

Could anybody help me. I need to create a script that reads a text file from STDIN and prints out the file to STDOUT with line numbers at the beginning of each line. Thanks. (5 Replies)
Discussion started by: mascorro
5 Replies
Login or Register to Ask a Question