in reply to Remove all lines, except those starting with pattern
The parentheses aren't optional. ^? means ^ (beginning of the string) 0 or 1 time; this will match if you are either at the beginning of the string, or if you are not... meaning everywhere. So your pattern actually is /!\d+\s+.*\n/ which is clearly not what you wanted.
While it's possible to do what you want with the -p option (read and print for each line, with possible modification), the -n option probably fits your problem better (read, but only print when you explictly ask for it), your one-liner becomes: perl -ne "print unless /^\d+\s+.*\n/"
Edit: or print if /regex/ if you want to keep only the lines with the pattern. I misread the title (didn't realise that "remove" + "except" works like a double negation, so print only the lines with the pattern). Thanks johngg
|
|---|