Match pattern and get lines numbers in a vector


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match pattern and get lines numbers in a vector
# 1  
Old 01-17-2013
Match pattern and get lines numbers in a vector

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
# 2  
Old 01-17-2013
Post samples of the input files and the desired output.
# 3  
Old 01-17-2013
file1:

Code:
ldap_bind_s
ldap_bind_s
ldap_bind_s
ldap_bind_proxy
ldap_bind_s
ldap_bind_s
... etc

file2:
Code:
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:
Code:
cat file1.txt | grep -n 'ldap_bind_proxy' | var=`cut -d ':' -f1` | sed -n '"$var"p' file2.txt

but something isn't going well

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.
# 4  
Old 01-17-2013
This will give you both the pattern and the record from the second file:

Code:
% paste file1 file2 | grep -F 'ldap_bind_proxy'
ldap_bind_proxy Helen Mayweather

With awk

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


Last edited by radoulov; 01-17-2013 at 07:57 AM..
This User Gave Thanks to radoulov For This Post:
# 5  
Old 01-17-2013
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
# 6  
Old 01-17-2013
This should work:
Code:
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.
# 7  
Old 01-17-2013
it worked perfectly!
thanks a lot!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

2. Shell Programming and Scripting

Printing next 6 lines from of pattern match

Hi, i have a big file having many opcodes. if (opcode="01110000000100000000" ) then --fadd result.opcode := "01110000000100000000"; result.s0 := '1'; result.s1 := '1'; result.s2 := '0'; result.inst := '0'; result.scalar := '1';... (7 Replies)
Discussion started by: twistedpair
7 Replies

3. Shell Programming and Scripting

Extract lines that match a pattern

Hi all, I got a file that contains the following content, Actually it is a part of the file content, Installing XYZ XYZA Image, API 18, revision 2 Unzipping XYZ XYZA Image, API 18, revision 2 (1%) Unzipping XYZ XYZA Image, API 18, revision 2 (96%) Unzipping XYZ XYZA Image, API 18,... (7 Replies)
Discussion started by: Kashyap
7 Replies

4. Shell Programming and Scripting

awk : match only the pattern string , not letters or numbers after that.

Hi Experts, I am finding difficulty to get exact match: file OPERATING_SYSTEM=HP-UX LOOPBACK_ADDRESS=127.0.0.1 INTERFACE_NAME="lan3" IP_ADDRESS="10.53.52.241" SUBNET_MASK="255.255.255.192" BROADCAST_ADDRESS="" INTERFACE_STATE="" DHCP_ENABLE=0 INTERFACE_NAME="lan3:1"... (6 Replies)
Discussion started by: rveri
6 Replies

5. Shell Programming and Scripting

Print lines that do not match the pattern

I need to print the lines that do not match a pattern. I tried using grep -v and sed -n '/pattern/!p', but both of them are not working as I am passing the pattern as variable and it can be null some times. Example ........ abcd...... .........abcd...... .........abcd......... (4 Replies)
Discussion started by: sunny1234
4 Replies

6. Shell Programming and Scripting

counting lines that match pattern

I have a file of 1.3 millions lines. some are with the same word twice on the line, some line have two diffrent words. each line has two words, one in brackets. example: foo (foo) bar (bar) thae (awvd) beladf (vswvw) I am sure this can be done with one line of... (6 Replies)
Discussion started by: robsonde
6 Replies

7. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

8. Shell Programming and Scripting

Print lines before and after pattern match

I am using Solaris, I want to print 3 lines before pattern match pattern 5 lines after pattern match Pattern is abcd to be searched in a.txt. Looking for the solution in sed/awk/perl. Thanks .. Input File a.txt: ================= 1 2 3 abcd 4 5 6 7 8 (7 Replies)
Discussion started by: manuswami
7 Replies

9. UNIX for Dummies Questions & Answers

retrieve lines that match a pattern

Hi, I would like to know how can I get lines from a text file that match no more than 2 '>'. Example: Input file: a >cr1 4 a>b b>c a >cr2 5 a>b Output file: a >cr2 5 a>b Thanks in advance (2 Replies)
Discussion started by: fadista
2 Replies

10. Shell Programming and Scripting

Match a pattern and copy above two lines

Dear experts, i want to search pattern tre and copy this line and above two lines in a seperate file:: Thanks for the help SEV="MAJOR": RX-TX HW-FAILURE DOMAIN="alcomc2_BSS_20 unit-type % bts nbr % 24 SBL-type % tre nbr % 4 subnb % 255 BR, Danish (16 Replies)
Discussion started by: Danish Shakil
16 Replies
Login or Register to Ask a Question