Mohd
Mohd

Reputation: 148

consecutive lines without changing order

cat file1
4    
8     
7  

cat file2
4.999286    12.669064   0.000000  
5.999343    12.753258   0.000000  
6.999401    12.654514   0.000000  
7.999458    12.774485   0.000000  
8.999515    12.662147   0.000000  
9.999572    12.700071   0.000000  
10.999629   12.717721   0.000000  

I am looking to grep file1 with the same order and two more consecutive lines. I tried this:

grep -A 2 -Ff file1 file2

I want the output like this:

4.999286    12.669064   0.000000  
5.999343    12.753258   0.000000  
6.999401    12.654514   0.000000  
8.999515    12.662147   0.000000  
9.999572    12.700071   0.000000  
10.999629   12.717721   0.000000  
7.999458    12.774485   0.000000  
8.999515    12.662147   0.000000  
9.999572    12.700071   0.000000 

Upvotes: 1

Views: 89

Answers (3)

RavinderSingh13
RavinderSingh13

Reputation: 133680

Could you please try following, this will give results in same order in which $1 occurs in Input_file1.

awk '
BEGIN{
  s1="\""
}
FNR==NR{
  a[$0]
  next
}
(int($1) in a){
  system("grep -A2 " s1 $0 s1 OFS FILENAME)
}
'  Input_file1  Input_file2


Explanation: Adding explanation for above code.

awk '                                             ##Starting awk program here.
BEGIN{                                            ##Starting BEGIN section of code here.
  s1="\""                                         ##Creating a variable named s1 whose value is "
}                                                 ##Closing BEGIN section of awk code here.
FNR==NR{                                          ##Checking condition if FNR==NR, which will be only TRUE when Input_file1 is being read.
  a[$0]                                           ##Creating an array named a whose index is $0.
  next                                            ##next will skip all further statements from here.
}                                                 ##Closing BLOCK for FNR==NR condition here.
(int($1) in a){                                   ##Checking condition if integer value of $1 is present in array a then do following.
  system("grep -A2 " s1 $0 s1 OFS FILENAME)       ##Using system command to run grep command which will print 2 lines after match of current line in current Input_file name passed to grep by FILENAME variable of awk.
}                                                 ##Closing BLOCK of condition.
'  Input_file1  Input_file2                       ##Mentioning Input_file names here.

Output will be as follows.

4.999286 12.669064 0.000000
5.999343 12.753258 0.000000
6.999401 12.654514 0.000000
7.999458 12.774485 0.000000
8.999515 12.662147 0.000000
9.999572 12.700071 0.000000
8.999515 12.662147 0.000000
9.999572 12.700071 0.000000
10.999629 12.717721 0.000000

Upvotes: 0

potong
potong

Reputation: 58488

This might work for you (GNU grep & parallel):

parallel -k grep -A2 '^{}' file2 :::: file1

Grep file2 multiple times in parallel using file1 as input and keep the original order.

Or if you prefer,as:

parallel -a file1 -k grep -A2 '^{}' file2

Or:

cat file1 | parallel -k grep -A2 '^{}' file2

Upvotes: 1

karakfa
karakfa

Reputation: 67537

grep is fast for few file scans

$ while IFS= read -r n; do grep -A2 "^$n." file2; done < file1

4.999286 12.669064 0.000000
5.999343 12.753258 0.000000
6.999401 12.654514 0.000000
8.999515 12.662147 0.000000
9.999572 12.700071 0.000000
10.999629 12.717721 0.000000
7.999458 12.774485 0.000000
8.999515 12.662147 0.000000
9.999572 12.700071 0.000000

Upvotes: 0

Related Questions