I want to create a sed command to find in a file the 1st line that matchs a pattern and the delete all that line or replace it all with some other text. I dont want to match all the line because the rule is to match part of it.
how can i do it with sed?
for instance:
myline 1 is pretty
line 2 is ugly
myline 111 is nice
I want to delete the first line that contains “1 is”
UPDATE: My line may have characters like “/” and “<”
Regards
Fak
I tend to use
awkfor more complicated tasks, it’s a bit more powerful thansed, having proper looping and selection constructs (expanded for readability, you can compress it back to one line if you wish):For any line not matching the pattern (
!/1 is/), it just prints it.For lines that do match the pattern, it prints them all if the flag
fis set (it’s initially not set). When the flags not set and it encounters a matching line, it sets the flag and doesn’t print it. This basically deletes the first matching line as desired.If you want to modify the first matching line rather than deleting it, you just insert code alongside the
f = 1to do that, something like:To use a shell variable in
awk, you can use the-voption to pass it in as a realawkvariable: