bug-bash
[Top][All Lists]
Advanced

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

Re: count


From: Greg Wooledge
Subject: Re: count
Date: Mon, 21 Dec 2015 09:28:21 -0500
User-agent: Mutt/1.4.2.3i

On Mon, Dec 21, 2015 at 07:58:02AM -0600, John McKown wrote:
> OOPS, slight correction:
> 
> find . -maxdepth 2 -mindepth 2 -type f -name '*.csv' -o -name '*.txt'  |\
> egrep '^\./[0-9]' |\
>  while read i;do echo -e "${PWD
> ???##???*/
> } $(dirname ${i
> ???
> }
> ??? | cut -b 3-???
> ) $(basename ${i}) $(wc -l ${i})" ;done | cut -d " " -f 1,2,4,3

This is such an unreadable mish-mash of UTF-8 gibberish and misplaced
newlines that I can barely see the bugs.

But yeah, there are bugs here.  This entire approach fails horribly on
filenames that contain newlines, and you're failing to quote, so it'll
also fail on filenames that contain whitespace or glob characters.

${i} is NOT a valid replacement for "$i".  Ever.

You're also adding unnecessary backslashes (a | character already
causes the command to continue on the next line, so |\ is NEVER needed),
and you're using egrep with a BRE.  These aren't bugs, though; they're
just unnecessary.

Since I can't quite tell what your code's supposed to be doing, I'll
just give an outline of an approach that should get you started, based
on Krem's original request.

Note that mine is done in ASCII, so you can actually feed it to a shell.
And read it in any not-a-web-browser mail program.  Unlike whatever the
hell that UTF-8 gibberish was.

find ./[0-9]* -type f \( -iname '*.csv' -o  -iname '*.txt' \) ... \
  -exec bash -c '
    for f; do
      IFS=/ read -ra part <<< "$f"
      printf "%-10.10s  %-10.10s  %-20.20s  %5d\n" \
        "${part[1]}" "${part[2]}" "${part[3]}" "$(wc -l < "$f")"
      ...
    done
' _ {} +

You'll probably want to adjust the field widths.  And I left the ./[0-9]*
thing in, but I don't know why it's there.  It doesn't seem to be part of
Krem's problem statement.  If it causes problems, just use find . instead.



reply via email to

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