[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: cat and pipelines, mostly (was Re: Delete all after a pattern)



On Sat 31 Aug 2019 at 10:07:01 (-0400), Roberto C. Sánchez wrote:
> On Sat, Aug 31, 2019 at 09:26:49AM -0400, The Wanderer wrote:
> > On 2019-08-31 at 09:02, Roberto C. Sánchez wrote:
> > 
> > > The only time you need to change the syntax is to add something
> > > before sed.  But then, that's why shells have I/O redirection:
> > > 
> > > (sed 's/config=.*$/config=/g' | tr -d '=') <~/test.txt
> > > >~/other_test.txt
> > 
> > This is *far* more complicated and messy, both syntactically and to type
> > in, than just having cat at the beginning of a pipeline.
> > 
> That's quite a stretch.
> 
> Your way:
> 
> cat ~/test.txt | sed 's/config=.*$/config=/g' | tr -d '=' >~/other_test.txt
> 
> A better way:
> 
> (sed 's/config=.*$/config=/g' | tr -d '=') <~/test.txt >~/other_test.txt
> 
> The better way is actually simpler.  It places all the logic for what is
> happening at the start of the line and the I/O redirection (which is
> basically just fluff) to the right.  Your way requires that I look at it
> for several moments to separate the I/O supporting pieces from the
> actual interesting command logic.  The better way has a clean visual
> separation.
> 
> > It also loses an important benefit when building and tweaking such
> > pipelines by hand: convenience of editing. In most shells with which I
> > have any experience, command history will place the cursor at the end of
> > the remembered line.
> > 
> 
> So, get a better shell?

You're not serious, are you. Learn another shell just to avoid a
"useless cat".

> > The further from the rightmost position the part you want to edit is,
> > the less convenient it is to do that editing, especially when doing
> > multiple trial-and-error passes to figure out what syntax will actually
> > produce the desired result.
> > 
> Again, it sounds like a better shell is in order.
> 
> > One item to the right of the final command (either redirection to a
> > file, or pipe to a pager) is usually unavoidable, at least with output
> > of any noticeable size; depending on your shell, there may be
> > keybindings for quick movement along the command line which reduce the
> > inconvenience again.
> > 
> 
> Here is an alternative that places the interesting commands as far to
> the right as possible:
> 
> $  i=~/test.txt; o=~/other_test.txt; (sed 's/config=.*$/config=/g' | tr -d '=') <${i} >${o}
> $  cat other_test.txt 
> Test config
> Test config
> Test config

So, using my example from earlier, I carefully type this into the
command line¹:

$ i=/tmp/sudsa; o=/dev/stdout; (sed 's/ 1/ /g' | tr -d '9') <${i} >${o}
 0 1 2 3 4
$ 

and then I remember there are a couple of files I'm interested in:

$ i=/tmp/suds?; o=/dev/stdout; (sed 's/ 1/ /g' | tr -d '9') <${i} >${o}
bash: ${i}: ambiguous redirect
$ 

Oops.

Who wants to have to reorganise the line while you're just doodling
about what patterns to use, on which files, and so on, at the command line.

¹ file contents:

$ my-listings /tmp/suds?
/tmp/sudsa :
9 10 11 12 13 14
/tmp/sudsb :
 15 16 17 18 19 20
$ 

Cheers,
David.


Reply to: