Pattern Match Using Perl-Append file contents

Wednesday, May 29, 2013 , 0 Comments

If i want to append all the lines of one file  to a line in second file only if there is a pattern match:

For example:
first_file.txt:
111111
1111
11
1

second_file.txt:

122221
2222
22
2

pattern to match:

2222
output:

122221
111111
1111
11
1
2222
111111
1111
11
1
22
2

Below is the perl command to achieve this:

perl -lne 'BEGIN{open(A,"first_file.txt");@f=<A>}
           print;if(/2222/){print @f}' 
           second_file.txt

0 comments: