Tuesday, May 7, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 526  / 3 Years ago, sat, october 2, 2021, 4:55:04

I have this input file:


text1
match
text2
match
text3

And I have this command:


perl -lpe 'print "prepend_me" if /^match$/' text.txt

And its output is:


text1
prepend_me
match
text2
prepend_me
match
text3

But I want:


text1
prepend_me
match
text2
match
text3

How do I get this?


More From » bash

 Answers
6

Just count how often it got already matched and prefix it only on the first match:


perl -lpe 'print "prepend_me" if /^match$/ && ++$count == 1' text.txt

[#2354] Sunday, October 3, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
uffno

Total Points: 283
Total Questions: 93
Total Answers: 111

Location: Saint Vincent and the Grenadines
Member since Thu, Oct 15, 2020
4 Years ago
uffno questions
Thu, Sep 2, 21, 18:08, 3 Years ago
Sun, Apr 2, 23, 15:15, 1 Year ago
Sat, Jan 8, 22, 16:37, 2 Years ago
Fri, Feb 10, 23, 02:11, 1 Year ago
;