I have this file input.txt:
Dog walks in the park
Man runs in the park
Man walks in the park
Dog runs in the park
Dog stays still
They run in the park
Woman runs in the park
I want to search for regular expression matches runs?and output them to a file, highlighting matches with two asterisks on either side of the match. Therefore my desired result is:
Man **runs** in the park
Dog **runs** in the park
They **run** in the park
Woman **runs** in the park
What I want to do is write a function that would be a wrapper for this one-line perl (and that would do a few other things), and then call it with a regex as its parameter. I wrote the following script:
#!/bin/bash
function reg {
perl -ne 's/($1)/**\1**/&&print' input.txt > regfunctionoutput.txt
}
function rega {
regex="$1"
perl -ne 's/($regex)/**\1**/&&print' input.txt > regafunctionoutput.txt
}
perl -ne 's/(runs?)/**\1**/&&print' input.txt > regularoutput.txt
reg 'runs?'
rega 'runs?'
The output of the first perl-liner is what I want. But when I try to wrap it in a function regand pass the expression as a parameter, instead of the desired output, I get:
****Dog walks in the park
****Man runs in the park
****Man walks in the park
****Dog runs in the park
****Dog stays still
****They run in the park
****Woman runs in the park
, $1 perl one-liner. rega, perl. , .
, perl one-liner ? ?