Select only line numbers using grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Select only line numbers using grep
# 1  
Old 06-11-2013
Select only line numbers using grep

Hai,

I want to select only line numbers into a file if some pattern matches. I have written my script like below but its not working.

Code:
#!/bin/sh
file='/home/testfile1'
filesearch='/home/test00'
while read line
do
search=`echo $line |cut -c 1-24`
echo $search
echo `grep -n "" $filesearch|awk 'BEGIN { FS = ":" } ; { print $1 }'` >> linenbrs

done < $file;

The above will read from filr testfile1 and search for the lines in test00 if it matches then i want the line number into linenbrs file

Here is my requirement :

Code:
cat testfile1
DISPLAY ' THIS IS FIRST LINE '
DISPLAY ' THIS IS SECOND LINE '' ITS CONTINUE '
DISPLAY ' THIS IS FOURTH LINE'

cat test00
DISPLAY ' THIS IS FIRST LINE '
DISPLAY ' THIS IS SECOND LINE '
' ITS CONTINUE '
DISPLAY ' THIS IS FOURTH LINE'

I WANT linenbrs FILE SHOULD HAVE LINE NUMBERS WHERE DISPLAY STARTED LIKE THIS :
$cat linenbrs
1
2
4

Plese write to me how to achieve this.

Thanks in advance..
# 2  
Old 06-11-2013
awk can do this fine, but I have trouble understanding what is the data to select from, and what is the search pattern.
# 3  
Old 06-11-2013
Your grep would need a change as below

Code:
grep -n "$line" $filesearch

# 4  
Old 06-11-2013
Hai ,

actually i need the line numbers in a file for further processing. I need to read data (line)from a a file(Here its testfile) and i have to search it in another file(filesearch). If found, i need to write that line number only into another file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Grep for a line containing only 5 numbers

How would you grep for a line containing only 5 numbers? Something like this. 10 2 12 1 13 (4 Replies)
Discussion started by: cokedude
4 Replies

2. Shell Programming and Scripting

How Select numbers from a line of text, and remove leading spaces?

I have a text file with a line of text that contains numbers and text formatted into groups. I need to extract the number that can be either 1,2 or 3 digits long. Then write it to a variable, but i need to remove any leading spaces in the number first. I can get the numbers out but how to remove... (12 Replies)
Discussion started by: kcpoole
12 Replies

3. UNIX for Dummies Questions & Answers

Grep lines with numbers greater than 2 digits at the end of the line

I'm trying to grep lines where the digits at the end of each line are greater than digits. Tried this but it will only allow me to specify 2 digits. Any ideas would greatly be appreciated. grep -i '\<\{3,4,5\}\>' file ---------- Post updated at 05:58 PM ---------- Previous update was at 05:41... (1 Reply)
Discussion started by: jimmyf
1 Replies

4. Shell Programming and Scripting

Grep to isolate a text file line and Awk to select a word?

I am looking at using grep to locate the line in the text file and them use awk to select a word or words out of it. I know awk needs -v to allow a variable to be used, but also needs -F to allow the break up of the sentence and allow the location of separate variables. $line = grep "1:" File |... (8 Replies)
Discussion started by: Ironguru
8 Replies

5. Shell Programming and Scripting

How do i select all contents between two numbers?

I want to select contents between two numbers say 1. and 2. from an output file which has many numbers but only the these two ending with a dot(.) eg 1. 2 . 32. etc I was looking to select with the use of a counter then modify the selected contents and put it in an output file where again the... (3 Replies)
Discussion started by: ausfragen
3 Replies

6. UNIX for Dummies Questions & Answers

grep to get line numbers

I know if i use grep -n that the output will have the lines numbered but is there a way to grep the actually line number. so like this grep -n "one" /usr/dict/numbers 1:one 21:twenty-one 31:thirty-one 41:forty-one 51:fifty-one 61:sixty-one 71:seventy-one 81:eighty-one 91:ninety-one ... (1 Reply)
Discussion started by: alindner
1 Replies

7. UNIX for Advanced & Expert Users

SQLPlus Select with Grep

I m trying to grep 'select * from' from a script, but the problem is with the * ... I think its taking * as some special character ... when I try to do grep '*' test (test is my test file) its displaying * But when I am trying to do grep 'select * from' test its not showing anything... (2 Replies)
Discussion started by: RDR
2 Replies

8. Shell Programming and Scripting

grep a pattern with line numbers.

I have a txt file with more than 10000 lines. There is a unique pattern which is scattered into the file. it starts with @9 and it has 15 characters. i need to grep them and display along with line numbers. Eg: File - Test1 test message.... .... .. .. @9qwerty89 ...test message... (8 Replies)
Discussion started by: abinash
8 Replies

9. UNIX for Advanced & Expert Users

How to select only those file names whose name contains only numbers

Hi Guru's, Before writing to this forum I have searched extensively on this forum about my problem. I have to write a shell script which takes out only those file names from the given directory which contains only numbers. For example, In the given directory these files are present: ... (4 Replies)
Discussion started by: spranm
4 Replies

10. UNIX for Dummies Questions & Answers

How to select only those file names whose name contains only numbers.

Hi Guru's, Before writing to this forum I have searched extensively on this forum about my problem. I have to write a shell script which takes out only those file names from the given directory which contains only numbers. For example, In the given directory these files are present: ... (0 Replies)
Discussion started by: spranm
0 Replies
Login or Register to Ask a Question