This is easy to do clearly in awk
:
echo "lost
load
linux
loan
linux" | awk '
/^li/ { found = 1 }
found { print }'
Here found
is a variable,
with an arbitrarily chosen, self-explanatory name.
It gets set when the program encounters an input line
that matches the regexp.
(Variables initially default to null,
which is functionally equivalent to 0 or FALSE.)
So input lines are printed after the ^li
pattern is matched,
and not before.
The third line of the input (the first linux
line) is printed
because the conditional print statement comes after
the statement that looks for the pattern and sets the flag.
If you want to start printing with the fourth line
(the line after first linux
line),
just reverse the order of the two statements.
If no input line matches the regexp,
the flag never gets set, and nothing is printed.
As I said, the name of the flag variable is arbitrary;
you can use something shorter (e.g., f
) if you want.
And { print }
is the default action, so you can leave it out.
So, if you don't care about clarity, you can shorten the above to
echo "lost
load
linux
loan
linux" | awk '/^li/{f=1}f'
sed -e '/linux/{:1;n;b1};d'
. You can see, inside the curly brace is just a loop, executen
command till the end of file. I broke it into many pieces for POSIX compliant. – cuonglm Jan 24 '16 at 15:22d
delete all before entering the loop when it's placed at the end of the code? And thed
command usually deletes only the matching part, right? e.g.sed '/linux/d'
Why can it delete all in this case? Sorry if this is a stupid question. – stacko Jan 24 '16 at 18:31n
isnt for printing pattern space (though it does happen by default) then
overwrites pattern space w/ the next input line if any. – mikeserv Jan 24 '16 at 19:46d
elete the lines. You don't have to used
elete command associate with a pattern, you can simplesed d
to delete all lines. – cuonglm Jan 25 '16 at 01:34