How to avoid perl -pe from printing unmatched lines - regex

The following will print out "chicken", which is expected:
echo "I like chicken" | perl -pe 's|.*(chicken).*|\1|'
Output: chicken
However, the following will print out "I like bacon" even if "chicken" was not matched:
echo "I like bacon" | perl -pe 's|.*(chicken).*|\1|'
Output: I like bacon
How can I avoid the unmatched string from being printed out?

s/// is for substitute, which I don't think is what you want to do. s|.*(chicken).*|\1| tells perl to find a line with 'chicken' in it, and replace the word 'chicken' with itself.
Instead use m// for regex matching, and use -ne instead of -pe:
echo "I like chicken" | perl -ne 'print if m|chicken|'
Output: I like chicken
echo "I like bacon" | perl -ne 'print if m|chicken|'
no output

Related

Perl - Match multiline pattern with wildcard

I have this example file test.txt:
aba
aca
ada
bcd
baa
I would like to find every occurence where a line starting with a and ending with a is followed by another such line. However, my attempts are futile:
perl -0777 -ne 'print "$1\n" while /(a.*a/n)/sg' test.txt
(...) # Matches the whole file as it starts with "a" and ends with "a\n"
perl -0777 -ne 'print "$1\n" while /(a.*?a\n)/sg' test.txt # What is this even
aba
aca
ada
aa
perl -0777 -ne 'print "$1\n" while /(a.*a\n){2}/sg' test.txt
aca
ada
bcd
baa
perl -0777 -ne 'print "$1\n" while /(a.*?a\n){2}/sg' test.txt
aca
These are my results on OSX. Can someone help me out here with the desired pattern?
Don't use the /s, so you can actually match lines. Also, add ^ and $ with /m to match line starts and ends:
perl -0777 -ne 'print "$1\n" while /^(a.*a)\n(a.*a)$/mg'
Or, process line by line remembering the previous line if it matches.
perl -lne '($q) = /^(a.*a)$/; print $p if $p && $q; $p = $q'
Note that the two solutions differ in how they treat more than two a.*a lines occurring sequentially. See ikegami's comment for an alternative solution.

One-liner to print all lines between two patterns

Using one line of Perl code, what is the shortest way possible to print all the lines between two patterns not including the lines with the patterns?
If this is file.txt:
aaa
START
bbb
ccc
ddd
END
eee
fff
I want to print this:
bbb
ccc
ddd
I can get most of the way there using something like this:
perl -ne 'print if (/^START/../^END/);'
That includes the START and END lines, though.
I can get the job done like this:
perl -ne 'if (/^START/../^END/) { print unless (/^(START)|(END)/); };' file.txt
But that seems redundant.
What I'd really like to do is use lookbehind and lookahead assertions like this:
perl -ne 'print if (/^(?<=START)/../(?=END)/);' file.txt
But that doesn't work and I think I've got something just a little bit wrong in my regex.
These are just some of the variations I've tried that produce no output:
perl -ne 'print if (/^(?<=START)/../^.*$(?=END)/);' file.txt
perl -ne 'print if (/^(?<=START)/../^.*(?=END)/);' file.txt
perl -ne 'print if (/^(?<=START)/../(?=END)/);' file.txt
perl -ne 'print if (/^(?<=START)/../.*(?=END)/);' file.txt
perl -ne 'print if (/^(?<=START)/../^.*(?=END)/);' file.txt
perl -ne 'print if (/^(?<=START)/../$(?=END)/);' file.txt
perl -ne 'print if (/^(?<=START)/../^(?=END)/);' file.txt
perl -ne 'print if (/^(?<=START)/../(?=^END)/);' file.txt
perl -ne 'print if (/^(?<=START)/../.*(?=END)/s);' file.txt
Read the whole file, match, and print.
perl -0777 -e 'print <> =~ /START.*?\n(.*?)END.*?/gs;' file.txt
May drop .*? after START|END if alone on line.
Then drop \n for a blank line between segments.
Read file, split line by START|END, print every odd of #F
perl -0777 -F"START|END" -ane 'print #F[ grep { $_ & 1 } (0..$#F) ]' file.txt
Use END { } block for extra processing. Uses }{ for END { }.
perl -ne 'push #r, $_ if (/^START/../^END/); }{ print "#r[1..$#r-1]"' file.txt
Works as it stands only for a single such segment in the file.
It seems kind of arbitrary to place a single-line restriction on this, but here's one way to do it:
$ perl -wne 'last if /^END/; print if $p; $p = 1 if /^START/;' file.txt
perl -e 'print split(/.*START.|END.*/s, join("", <>))' file.txt
perl -ne 'print if /START/../END/' file.txt | perl -ne 'print unless $.==1 or eof'
perl -ne 'print if /START/../END/' file.txt | sed -e '$d' -n -e '1\!p'
I don't see why you are so insistent on using lookarounds, but here are a couple of ways to do it.
perl -ne 'print if /^(?=START)/../^(?=END)/'
This finds the terminators without actually matching them. A zero-length match which satisfies the lookahead is matched.
Your lookbehind wasn't working because it was trying to find beginning of line ^ with START before it on the same line, which can obviously never match. Factor the ^ into the zero-width assertion and it will work:
perl -ne 'print if /(?<=^START)/../(?<=^END)/'
As suggested in comments by #ThisSuitIsBlackNot you can use the sequence number to omit the START and END tokens.
perl -ne '$s = /^START/../^END/; print if ($s>1 && $s !~ /E0/)'
The lookarounds don't contribute anything useful so I did not develop those examples fully. You can adapt this to one of the lookaround examples above if you care more about using lookarounds than about code maintainability and speed of execution.

search and replace regexp gives two different outputs if grouping metacharacters are used

I get different outputs from search and replace regexp in perl depending whether I use in place replace (sed alternative) or regular search replace and also depending on whether I use \1 or $1:
──> cat test1.txt
orig.avg.10
──> cat test2.txt
orig.avg.10
# EXPECTED
──> cat test1.txt | perl -lne '$_ =~ s/(avg\.[0-9]+)/$1\.vec/; print $_'
orig.avg.10.vec
# EXPECTED
──> cat test1.txt | perl -lne '$_ =~ s/(avg\.[0-9]+)/\1\.vec/; print $_'
orig.avg.10.vec
# EXPECTED
──> perl -p -i.bak -e "s/(avg\.[0-9]+)/\1\.vec/" test2.txt
──> cat test2.txt
orig.avg.10.vec
# UNEXPECTED
──> perl -p -i.bak -e "s/(avg\.[0-9]+)/$1\.vec/" test1.txt
──> cat test1.txt
orig..vec
Why this happens?
You are using " to wrap your perl code, but doing so means the shell can and will interpolate $1.
Use ' instead and everything will work as expected.
The problem is that I've used in #UNEXPECTED case double quotes which makes expanding $1 variable. Sometimes one need to write down the questions before realized the case.

Perl one-liner to match all occurrences of regex

For multiple lines of text similar to this:
"views_panes","gw_hero_small_site_placement-panel_pane_1",1,"a:0:{}","a:10:{s:14:\"override_title\";i:1;s:19:\"override_title_text\";s:0:\"\";s:9:\"view_mode\";s:11:\"all_purpose\";s:11:\"image_style\";s:7:\"default\";s:13:\"style_options\";a:2:{s:10:\"show_image\";i:0;s:9:\"show_date\";i:0;}s:18:\"gw_display_options\";s:22:\"gw_all_purpose_sidebar\";s:13:\"show_readmore\";a:1:{s:18:\"show_readmore_link\";i:0;}s:14:\"readmore_title\";s:9:\"Read more\";s:13:\"readmore_link\";s:0:\"\";s:7:\"exposed\";a:1:{s:23:\"field_hero_sub_type_tid\";s:3:\"547\";}}","a:0:{}","a:1:{s:8:\"settings\";N;}","a:0:{}","a:0:{}",0,"s:0:\"\";"
I am looking to match all instances of (s:)(\d{1,}:)\"(string)\"; to get something like this:
s:14:override_title
s:18:show_readmore_link
s:3:547
This line with or without /g prints only the first instances:
perl -nle 'print "$1 $2 $3" if /(s:)(\d{1,}:)\\"(.*?)\\";/g' tmp.txt
s:14:override_title
I suppose I can try to put this in a perl script putting all matches into an array, but am hoping to do this using a one-liner (-: What am I missing?
Mac OS X 10.7.5, perl 5.12.3.
It's seem you have only line, so have a try with:
perl -nle 'print "$1 $2 $3" while(/.*?(s:)(\d{1,}:)\\"(.*?)\\";/g)' tmp.txt

How to use variable in regex?

How do I replace regex with $var in this command?
echo "$many_lines" | sed -n '/regex/{g;1!p;};h'
$var could look like fs2#auto-17.
The sed command will output the line immediately before a regex, but not the line containing the regex.
If all this can be done easier with a Perl one-liner, then it is fine with me.
It is not beautiful, but this gives me the previous line to $var which is want I wanted.
echo "$many_lines" | grep -B1 "$var" | grep -v "$var"
In Perl regexes, you can interpolate variable contents into regexes like /$foo/. However, the contents will be interpreted as a pattern. If you want to match the literal content of $foo, you have to escape the metacharacters: $safe = quotemeta $foo; /$safe/. This can be shortended to /\Q$foo\E/, which is what you usually want. A trailing \E is optional.
I don't know if the sed regex engine has a similar feature.
A Perl one-liner: perl -ne'$var = "..."; print $p if /\Q$var/; $p=$_'
Use double quotes instead of single quotes to allow variable expansion:
echo $many_lines | sed -n "/$var/"'{g;1!p;};h'
Since you are looking for a line before the regex, with a single one liner it will not be that trivial and beautiful, but here is how I will do it (Using Perl only):
echo "$many_lines" | perl -nle 'print $. if /\Q$var/' | while read line_no; do
export line_no
echo $many_lines | perl -nle 'print if $. - 1 == $ENV{line_no}'
done
or if you want to do in one line
echo "$many_lines" | perl -nle 'BEGIN {my $content = ""; } $content .= $_; END { while ($content =~ m#([^\n]+)\n[^\n]+\Q$var#gosm) { print $1 }}'
Or this one, should definitely match:
echo "$many_lines" | perl -nle 'BEGIN {my #contents; } push #contents, $_; if ($contents[-1] =~ m#\Q$var#o)') { print $contents[-2] if defined $contents[-2]; }
Or you can use here-documents too, if you don't want to escape the double quotes!
In Perl it looks like this:
$heredoc = <<HEREDOC;
here is your text and $var is your parameter
HEREDOC
Its important to end the heredoc with the same string you began, in my example its "HEREDOC" in a new line!