On 2019-08-31 at 07:58, Roberto C. Sánchez wrote:

> On Sat, Aug 31, 2019 at 01:49:20PM +0200, Computer Planet wrote:
> 
>> Hi guys! Is It possible, with "sed" erase all after a pattern? I'm
>> trying in all way but I can't... I'd like to erase all after the
>> pattern "config=" but only in the same line, regardless of where it
>> is located inside in a file.
>> 
>> Can somebody help me please? Thank in advance for reply.
>> 
>> e.g.: after "config=" erase all until the end of the line
> 
> Something like this:
> 
> sed -E 's/(.*config=).*/\1/'

Or perhaps

sed 's/config=.*$/config=/g'

?

Less elegant and idiomatic, but could also get the job done.

The 'g' at the end is in case there can be multiple occurrences of
'config=' in a single file, so that sed won't stop after the first one
it finds.


In practice, I'd either use this with 'sed -i [the above expression]
filename' or (more likely) with 'cat filename | sed [the above
expression] > newfilename'.

(Yes, that's technically a "senseless use of cat". I do it anyway,
because always using pipes at every stage makes it easy to add or remove
filtering stages without having to adjust the syntax in another part of
the pipeline, and because it's easier to stick with that habitual
pattern than to change it up in the relatively few cases where I can be
sure that multiple stages aren't and won't be needed.)

(And may I say that it's annoying to need to explain this every time, in
order to forestall being called out for "senseless use of cat"? Not that
I get called out for that here very much, but it does seem to happen
virtually every time I don't include an explanation...)

-- 
   The Wanderer

The reasonable man adapts himself to the world; the unreasonable one
persists in trying to adapt the world to himself. Therefore all
progress depends on the unreasonable man.         -- George Bernard Shaw

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to