# Match pattern and get lines numbers in a vector

**URL:** <https://community.unix.com/t/match-pattern-and-get-lines-numbers-in-a-vector/322810>\
**Category:** Shell Programming and Scripting\
**Created:** [January 17, 2013, 4:14am UTC](https://community.unix.com/t/match-pattern-and-get-lines-numbers-in-a-vector/322810 "2013-01-17T04:14:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dimcick](https://community.unix.com/letter_avatar/dimcick/32/5_5575768a8748004e209b776fc1b2916d.png) [@dimcick](https://community.unix.com/u/dimcick)\
**Post date:** [January 17, 2013, 4:14am UTC](https://community.unix.com/t/match-pattern-and-get-lines-numbers-in-a-vector/322810/1 "2013-01-17T04:14:59Z")

</div>

Hi everyone,  
I am new to shell scripting, and would appreciate your help on following problem.

I need to search a file for a pattern, then get the number of each line that matches the given pattern.  
Then I need to search those specific line numbers from the first file in a second file and print the entire lines.

I tried with grep -n 'pattern' from the first file, which gives me:

21: pattern  
37: pattern  
58: pattern  
...

now I need to get 21,37,58... in a vector or something, and display these lines from the second file.

I hope I made myself clear.

thanks in advance

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [January 17, 2013, 4:33am UTC](https://community.unix.com/t/match-pattern-and-get-lines-numbers-in-a-vector/322810/2 "2013-01-17T04:33:31Z")

</div>

Post samples of the input files and the desired output.

---

<div class="post-metadata">

**Author:** ![dimcick](https://community.unix.com/letter_avatar/dimcick/32/5_5575768a8748004e209b776fc1b2916d.png) [@dimcick](https://community.unix.com/u/dimcick)\
**Post date:** [January 17, 2013, 6:22am UTC](https://community.unix.com/t/match-pattern-and-get-lines-numbers-in-a-vector/322810/3 "2013-01-17T06:22:20Z")

</div>

file1:

```nohighlight
ldap_bind_s
ldap_bind_s
ldap_bind_s
ldap_bind_proxy
ldap_bind_s
ldap_bind_s
... etc

```

file2:

```nohighlight
John Smith
Martin King
Octavius Papadocoluos
Helen Mayweather
Johnatan Stick
...etc

```

I need to search in file1 for pattern=ldap\_bind\_proxy, and display the name of the person at the same line number from file2  
e.g. in above example, it would be Helen Mayweather, located at line number 4

Thank you

---------- Post updated at 01:22 PM ---------- Previous update was at 01:20 PM ----------

I tried:

```nohighlight
cat file1.txt | grep -n 'ldap_bind_proxy' | var=`cut -d ':' -f1` | sed -n '"$var"p' file2.txt

```

but something isn't going well

---

<div class="post-metadata">

**Author:** ![radoulov](https://community.unix.com/user_avatar/community.unix.com/radoulov/32/489_2.png) [@radoulov](https://community.unix.com/u/radoulov)\
**Post date:** [January 17, 2013, 6:31am UTC](https://community.unix.com/t/match-pattern-and-get-lines-numbers-in-a-vector/322810/4 "2013-01-17T06:31:49Z")

</div>

This will give you both the pattern and the record from the second file:

```nohighlight
% paste file1 file2 | grep -F 'ldap_bind_proxy'
ldap_bind_proxy Helen Mayweather

```

With awk

```nohighlight
awk 'NR == FNR {
  $0 ~ p && nr = NR 
  next
  }
FNR == nr
  ' p='ldap_bind_proxy' file1 file2

```

---

<div class="post-metadata">

**Author:** ![dimcick](https://community.unix.com/letter_avatar/dimcick/32/5_5575768a8748004e209b776fc1b2916d.png) [@dimcick](https://community.unix.com/u/dimcick)\
**Post date:** [January 17, 2013, 6:56am UTC](https://community.unix.com/t/match-pattern-and-get-lines-numbers-in-a-vector/322810/5 "2013-01-17T06:56:57Z")

</div>

Thank you radoulov. for your reply.  
Can you please explain a bit, the code meaning, because it only displays only the last match and not all of them, so I can modify it accordingly.

thanks

---

<div class="post-metadata">

**Author:** ![radoulov](https://community.unix.com/user_avatar/community.unix.com/radoulov/32/489_2.png) [@radoulov](https://community.unix.com/u/radoulov)\
**Post date:** [January 17, 2013, 7:01am UTC](https://community.unix.com/t/match-pattern-and-get-lines-numbers-in-a-vector/322810/6 "2013-01-17T07:01:02Z")

</div>

This should work:

```nohighlight
awk 'NR == FNR {
  $0 ~ p && nr[NR] 
  next
  }
FNR in nr
  ' p='ldap_bind_proxy' file1 file2

```

While reading the first input file ( `NR ==FNR` ), build an array named nr containing the record numbers of the matching records.  
While reading the second input file, print the records whose record numbers match the keys of the array nr.

---

<div class="post-metadata">

**Author:** ![dimcick](https://community.unix.com/letter_avatar/dimcick/32/5_5575768a8748004e209b776fc1b2916d.png) [@dimcick](https://community.unix.com/u/dimcick)\
**Post date:** [January 17, 2013, 7:03am UTC](https://community.unix.com/t/match-pattern-and-get-lines-numbers-in-a-vector/322810/7 "2013-01-17T07:03:56Z")

</div>

it worked perfectly!  
thanks a lot!
