re: Processing Text with Linux Shell - Part 1 VIEW POST

FULL DISCUSSION
 

Why rescan the input 4-times to change the l,i,n,u,x? Instead, use sed ‘s/L/l/;s/n/N/;s/l/L/;s/x/X/‘?

I’ll have to play w/-i … it feels dangerous 😟 -i.bak makes me feel more comfortable, though. 😊

I spent a couple of hours yesterday writing a 1-liner listing a java project’s exposed endpoints and their HTTP methods, based on @.*Mapping annotations.

 
 

Why rescan the input 4-times to remove the l,i,n,u,x? Instead, use sed ‘s/L/l/;s/n/N/;s/l/L/;s/x/X/‘?

Guess I just missed that one.

I’ll have to play w/-i … it feels dangerous 😟 -i.bak makes me feel mkre comfortsble, though.

It is indeed. Once I messed up some logs files that I was going through.

 

It's just a demonstration of piping, I don't think it's intended to be optimal. You could just do echo Linux | tr 'nx' 'NX' if you wanted that.

 

Similarly, have single-invocation choices like:

echo Linux | sed -e 's/L/l/'-e 's/n/N/'-e 's/l/L/' -e 's/x/X/'

Which, to me, is more readable than having everything all bunched up and lends itself to uniform line-breaking in a long, complex script. Similarly, if you go this route, you also improve readability while retaining the "single invocation" efficiencies.

echo Linux | sed '{
   s/L/l/
   s/n/N/
   s/l/L/
   s/x/X/
}

...Though, if you're transforming the "L" to an "l" and then back to an "L", efficiency likely isn't your goal.

Whether using multiple, discreet sed invocations or a single, multi-transform sed, always remember that "order counts".

 

if you're transforming the "L" to an "l" and then back to an "L", efficiency likely isn't your goal.

intention was just to demonstrate piping :)

code of conduct - report abuse