help-bash
[Top][All Lists]
Advanced

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

Re: Printing arbitrary line range from files


From: Seth David Schoen
Subject: Re: Printing arbitrary line range from files
Date: Mon, 28 Jun 2021 00:35:07 -0700

lisa-asket@perso.be writes:

> Does not function very well when I do
> 
> print-lines 5 8 .
> 
> print-lines ()
>   {
>     na=$1
>     nb=$2
>     dir=$3
> 
>     find "$dir" \( -name \*.org -o -name \*.texi \)  \
>       | xargs sed -n "$na,${nb}p"
>   }

I see two things that could easily go wrong:

* You probably want -print0 in find, and -0 in xargs (so that it will
  work properly for files whose names contain spaces).

* You probably want -n 1 in xargs so that it will run the sed command
  once per file, instead of (likely) once total.  xargs constructs
  command lines using multiple file arguments per command, unless you
  use -n to limit this.  When you want it to run the command separately
  for each individual file, you then need -n 1.

The reason for the second behavior is that many programs will take
multiple file arguments and do the expected thing with all of them.  If
you wanted to compress a lot of files with gzip, you could use

find /somedir -print0 | xargs -0 gzip

and xargs would run gzip "as few times as possible" in some sense, but
gzip would still compress each file.

-- 
Seth David Schoen <schoen@loyalty.org>      |  Qué empresa fácil no pensar
     http://www.loyalty.org/~schoen/        |  en un tigre, reflexioné.
                                            |        -- Borges, "El Zahir"



reply via email to

[Prev in Thread] Current Thread [Next in Thread]