I have two files, File1 and File2. I would like to compare lines between these two files and print. However I do not find a way to match specific string. I tried the coding below, but it prints out unexpected result.
I would like to print File2 data (eg. b05ldt10ud0e0) when it matches to File1's names(eg. ldt). For those data with asterisk * in File1 (eg. b05can03*n0b5), * could be any character. For matching, It must match head and tail of an * in File2.Thus, it will print out b05can03un0b5. And printing must be follow the sequence of File1. Could anyone give me advice on this?
File1
--------------------------
ldt
b05dcc00
mny
b05can03*n0b5
b05mdd04*n9c9
File2
---------------------------
/* To start: b05afn10ud0b0 */
/* To start: b05dcc00ud0c0 */
/* To start: b05ldt10ud0e0 */
/* To start: b05dcc10ud0i0 */
/* To start: b05afn10ud0m0 */
/* To start: b05afn10ud0s0 */
/* To start: b05mny00ud0b5 */
/* To start: b05mny00ud0d3 */
/* To start: b05mdd04un9c9 */
/* To start: b05ahn00ud0j5 */
/* To start: b05mny00ud0m7 */
/* To start: b05can03un0b0 */
/* To start: b05can03un0b5 */
Coding:
-----------------------
my (@arr1,@arr2); @arr1=<File1>; @arr2=<File2>; foreach my $line1 (@arr1) { foreach my $line2 (@arr2){ if ($line1 =~ $line2 && $line1 !~ m/^\w+(\W)\w+(.*)/) { print "$line2\n";` } } }
expected result:
b05ldt10ud0e0
b05dcc00ud0c0
b05mny00ud0b5
b05mny00ud0d3
b05mny00ud0m7
b05can03un0b5
b05mdd04un9c9
In reply to How to match specific character in a string? by WWq
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |