Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.os.linux.advocacy > #412694

Re: Snit hitting the glue bag early today

From owl <owl@rooftop.invalid>
Newsgroups comp.os.linux.advocacy
Subject Re: Snit hitting the glue bag early today
Date 2017-05-03 14:07 +0000
Organization O.W.L.
Message-ID <uzv003.aui@rooftop.invalid> (permalink)
References (6 earlier) <oe8d2o$28o$1@dont-email.me> <xv803a.bhu@rooftop.invalid> <oea874$j9$2@dont-email.me> <acv03.abhuu4@rooftop.invalid> <oeclcq$tll$3@dont-email.me>

Show all headers | View raw


DFS <nospam@dfs.com> wrote:
> On 5/2/2017 6:12 PM, owl wrote:
> 
> 
>> anon@lowtide:~/code/repeat$ cat repeat
>> #!/bin/bash
>> while IFS= read -r line;do
>> count=$(echo ${line}|sed -e 's/.*\s\([0-9]*\)$/\1/g')
>> echo -n "${line}    ";for ((i=0;i<${count};i++));do echo -n "${1}";done;echo
>> done
> 
> Instead of using sed, can't your 3rd line just return int() of the last 
> 2 non-newline characters in the input?
> 

This works with any width number though.  Bash substring expansion
would work too, but this gives error if it's not a digit.


> As written, the python fails at anything above 99.  I thought about 
> limiting the whole output to 80, since wrapping makes the bars visually 
> useless anyway.
> 
> Edit: and here's how easy it is to limit to length N in the masterful 
> python language:
> 
> just paren the last line and slice it to N chars.  No new lines of code.
> 
> (s.rstrip()+'  '+''.join(sys.argv[2])*int(s.rstrip()[-2:]))[:int(N)]
> 
> then you pass the max line length as an argument:
> 
> import sys
> for s in open(sys.argv[1],'r').readlines():
>        print (s.rstrip()+' 
> '+''.join(sys.argv[2])*int(s.rstrip()[-2:]))[:int(sys.argv[3])]
> 
> $ python repeat.py data.txt $ 70
> 
> 00    5    $$$$$
> 01    2    $$
> 08    5    $$$$$
> 09   37    $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
> 10   30    $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
> 11   99    $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
> 12   21    $$$$$$$$$$$$$$$$$$$$$
> 13   21    $$$$$$$$$$$$$$$$$$$$$
> 14    7    $$$$$$$
> 15   20    $$$$$$$$$$$$$$$$$$$$
> 16   27    $$$$$$$$$$$$$$$$$$$$$$$$$$$
> 17   33    $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
> 18   26    $$$$$$$$$$$$$$$$$$$$$$$$$$
> 19   48    $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
> 20   13    $$$$$$$$$$$$$
> 21    1    $
> 22    2    $$
> 
> At this point I'd rewrite:
> 
> import sys
> for line in open(sys.argv[1],'r').readlines():
>        data    = line.rstrip()
>        barchar = ''.join(sys.argv[2])
>        barlen  = int(data[-2:])
>        maxlen  = int(sys.argv[3])
>        print (data +'    '+ (barchar*barlen))[:maxlen]
> 
> 
> bash == pwned
> C    == pwned
> 
> 
>> Slower than python, though.
> 
> That's OK.
> 
> python: 0m0.025s
> Bash  : 0m0.086s
> C     : 0m0.001s
> 
> Inconsequential in everyday usage.
> 

Depends on how many lines in the data, how many data files
you need to process, and how often you have to do it.

> 
> 
>>> py rulez:
>>> ---------------------------------------------------------------------
>>> import sys
>>> for s in open(sys.argv[1],'r').readlines():
>>>      print s.rstrip()+'    '+''.join(sys.argv[2])*int(s.rstrip()[-2:])
>>> ---------------------------------------------------------------------
>>>
> 
>> No, C rules.
> 
> Not for this.
> 

Unless speed matters.

> code?
> 
> 

------------------------- rep.c ----------------------------

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAXLINES 400 
#define MAXCHARS 100
#define MAXINTS 100
#define BUFMAX 10

int main(int argc, char *argv[])
{
  int c,i,j,n,p;
  int linecount;
  FILE *fp;
  char *filename;
  char *mark=NULL;
  char lines[MAXLINES][MAXCHARS]={{0}};
  int nums[MAXINTS];
  char buf[BUFMAX]={0};
  if(argc!=3)
  {
    fprintf(stderr,"usage: %s <filename> <mark>\n",argv[0]);
    exit(1);
  }
  filename=argv[1];
  mark=argv[2];
  fp=fopen(filename,"r+");
  if(fp==NULL)
  {
    fprintf(stderr,"fp==NULL\n");
    exit(1);
  }
  while((c=fgetc(fp))!=EOF)
  {
    if(c=='\n')
    {
       lines[i][j++]='\0';
       linecount++; 
       if(linecount>=MAXLINES)
       {
          fprintf(stderr,"too many lines\n");
          fclose(fp);
          exit(1);
       }
       i++;
       j=0;
    }
    else
    {
       lines[i][j++]=c;
    }
  }
  for(i=0;i<linecount;i++)
  {
    for(j=0;lines[i][j]!='\0';j++)
    {
       while(lines[i][j]!=' ')
       {
           j++;
       }
       while(lines[i][j]==' ')
       {
           j++;
       }
       n=0;
       for(p=0;p<BUFMAX;p++)
       {
           buf[p]='\0';
       }
       while(lines[i][j]!='\0')
       {
           if(isdigit(lines[i][j]))
           { 
              buf[n]=lines[i][j];
           }
           else
           {
              buf[n]='\0';
           }
           n++;
           j++;
       }
        nums[i]=atoi(buf);
    }
  }
  for(i=0;i<linecount;i++)
  {
        printf("%s ",lines[i]); 
        for(j=0;j<nums[i];j++)
        {
           printf("%s",mark);
        }
        printf("\n");
  }
  fclose(fp);
  return 0;
}


------------------------------------------------------------

Back to comp.os.linux.advocacy | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Re: Snit hitting the glue bag early today Sandman <mr@sandman.net> - 2017-05-01 07:10 +0000
  Re: Snit hitting the glue bag early today Steve Carroll <fretwizzer@gmail.com> - 2017-05-01 07:17 -0700
    Re: Snit hitting the glue bag early today Glenn Harrison <glannharrison0@gmail.com> - 2017-05-01 16:24 +0000
      Re: Snit hitting the glue bag early today Snit <usenet@gallopinginsanity.com> - 2017-05-01 09:38 -0700
        Re: Snit hitting the glue bag early today Sandman <mr@sandman.net> - 2017-05-01 21:51 +0000
          Re: Snit hitting the glue bag early today Snit <usenet@gallopinginsanity.com> - 2017-05-01 15:14 -0700
          Re: Snit hitting the glue bag early today vallor <vallor@cultnix.org> - 2017-05-01 22:53 +0000
            Re: Snit hitting the glue bag early today Snit <usenet@gallopinginsanity.com> - 2017-05-01 16:34 -0700
    Re: Snit hitting the glue bag early today Sandman <mr@sandman.net> - 2017-05-01 18:00 +0000
      Re: Snit hitting the glue bag early today DFS <nospam@dfs.com> - 2017-05-01 18:35 -0400
        Re: Snit hitting the glue bag early today owl <owl@rooftop.invalid> - 2017-05-02 02:23 +0000
          Re: Snit hitting the glue bag early today DFS <nospam@dfs.com> - 2017-05-02 11:26 -0400
            Re: Snit hitting the glue bag early today owl <owl@rooftop.invalid> - 2017-05-02 22:12 +0000
              Re: Snit hitting the glue bag early today DFS <nospam@dfs.com> - 2017-05-03 09:22 -0400
                Re: Snit hitting the glue bag early today owl <owl@rooftop.invalid> - 2017-05-03 14:07 +0000
                Re: Snit hitting the glue bag early today DFS <nospam@dfs.com> - 2017-05-04 09:29 -0400
                Re: Snit hitting the glue bag early today Sandman <mr@sandman.net> - 2017-05-04 16:42 +0000
                Re: Snit hitting the glue bag early today owl <owl@rooftop.invalid> - 2017-05-04 20:06 +0000
                Re: Snit hitting the glue bag early today owl <owl@rooftop.invalid> - 2017-05-04 20:12 +0000
                Re: Snit hitting the glue bag early today chrisv <chrisv@nospam.invalid> - 2017-05-05 06:52 -0500
                Re: Snit hitting the glue bag early today Marek Novotny <marek.novotny@marspolar.com> - 2017-05-05 10:32 -0500
                Re: Snit hitting the glue bag early today DFS <nospam@dfs.com> - 2017-05-05 13:22 -0400
                Re: Snit hitting the glue bag early today DFS <nospam@dfs.com> - 2017-05-05 13:01 -0400
                Re: Snit hitting the glue bag early today Steve Carroll <fretwizzer@gmail.com> - 2017-05-04 13:16 -0700
                Re: Snit hitting the glue bag early today Sandman <mr@sandman.net> - 2017-05-04 21:36 +0000
                Re: Snit hitting the glue bag early today owl <owl@rooftop.invalid> - 2017-05-04 19:49 +0000
                Re: Snit hitting the glue bag early today DFS <nospam@dfs.com> - 2017-05-04 19:28 -0400
                Re: Snit hitting the glue bag early today owl <owl@rooftop.invalid> - 2017-05-05 02:56 +0000
                Re: Snit hitting the glue bag early today DFS <nospam@dfs.com> - 2017-05-04 23:40 -0400
                Re: Snit hitting the glue bag early today owl <owl@rooftop.invalid> - 2017-05-05 06:18 +0000
                Re: Snit hitting the glue bag early today owl <owl@rooftop.invalid> - 2017-05-05 06:34 +0000
                Re: Snit hitting the glue bag early today Sandman <mr@sandman.net> - 2017-05-05 07:07 +0000
                Re: Snit hitting the glue bag early today owl <owl@rooftop.invalid> - 2017-05-05 07:29 +0000
                Re: Snit hitting the glue bag early today owl <owl@rooftop.invalid> - 2017-05-05 16:48 +0000
                Re: Snit hitting the glue bag early today DFS <nospam@dfs.com> - 2017-05-05 13:02 -0400
                Re: Snit hitting the glue bag early today DFS <nospam@dfs.com> - 2017-05-05 13:00 -0400
                Re: Snit hitting the glue bag early today Chris Ahlstrom <OFeem1987@teleworm.us> - 2017-05-05 05:28 -0400
                Re: Snit hitting the glue bag early today DFS <nospam@dfs.com> - 2017-05-05 13:02 -0400
    Re: Snit hitting the glue bag early today DFS <nospam@dfs.com> - 2017-05-01 14:44 -0400
      Re: Snit hitting the glue bag early today Snit <usenet@gallopinginsanity.com> - 2017-05-01 11:50 -0700
      Re: Snit hitting the glue bag early today Steve Carroll <fretwizzer@gmail.com> - 2017-05-01 12:12 -0700
        Re: Snit hitting the glue bag early today DFS <nospam@dfs.com> - 2017-05-01 17:37 -0400
          Re: Snit hitting the glue bag early today Sandman <mr@sandman.net> - 2017-05-01 22:06 +0000
            Re: Snit hitting the glue bag early today chrisv <chrisv@nospam.invalid> - 2017-05-02 08:08 -0500
              Re: Snit hitting the glue bag early today DFS <nospam@dfs.com> - 2017-05-02 11:26 -0400
              Re: Snit hitting the glue bag early today Silver Slimer <sl@im.er> - 2017-05-02 12:25 -0400
                Re: Snit hitting the glue bag early today Snit <usenet@gallopinginsanity.com> - 2017-05-02 09:52 -0700
                Re: Snit hitting the glue bag early today Sandman <mr@sandman.net> - 2017-05-02 19:39 +0000
                Re: Snit hitting the glue bag early today Snit <usenet@gallopinginsanity.com> - 2017-05-02 14:14 -0700
                Snit digest 432 / 2017-05-02 Sandman <mr@sandman.net> - 2017-05-02 21:37 +0000
              Re: Snit hitting the glue bag early today "Alistair Fitzpatrick" <fitz@gmail.com> - 2017-05-03 09:32 -0400
                Re: Snit hitting the glue bag early today chrisv <chrisv@nospam.invalid> - 2017-05-03 08:55 -0500
                Re: Snit hitting the glue bag early today DFS <nospam@dfs.com> - 2017-05-03 10:20 -0400
                Re: Snit hitting the glue bag early today William Poaster <wp@dev.null> - 2017-05-03 15:22 +0100
            Re: Snit hitting the glue bag early today DFS <nospam@dfs.com> - 2017-05-02 10:28 -0400
          Re: Snit hitting the glue bag early today Silver Slimer <sl@im.er> - 2017-05-01 19:25 -0400
        Re: Snit hitting the glue bag early today Sandman <mr@sandman.net> - 2017-05-01 21:50 +0000
  Re: Snit hitting the glue bag early today Snit <usenet@gallopinginsanity.com> - 2017-05-01 08:11 -0700
    Re: Snit hitting the glue bag early today Sandman <mr@sandman.net> - 2017-05-01 18:06 +0000
      Re: Snit hitting the glue bag early today Snit <usenet@gallopinginsanity.com> - 2017-05-01 11:48 -0700
        Re: Snit hitting the glue bag early today Sandman <mr@sandman.net> - 2017-05-01 19:20 +0000
          Re: Snit hitting the glue bag early today Snit <usenet@gallopinginsanity.com> - 2017-05-01 12:31 -0700
            Re: Snit hitting the glue bag early today Sandman <mr@sandman.net> - 2017-05-01 21:37 +0000
              Re: Snit hitting the glue bag early today Snit <usenet@gallopinginsanity.com> - 2017-05-01 15:14 -0700
                Re: Snit hitting the glue bag early today Sandman <mr@sandman.net> - 2017-05-02 06:28 +0000
                Re: Snit hitting the glue bag early today Sandman <mr@sandman.net> - 2017-05-02 06:47 +0000
                Re: Snit hitting the glue bag early today vallor <vallor@cultnix.org> - 2017-05-02 06:53 +0000
                Re: Snit hitting the glue bag early today Sandman <mr@sandman.net> - 2017-05-02 07:10 +0000
                Re: Snit hitting the glue bag early today Snit <usenet@gallopinginsanity.com> - 2017-05-02 09:52 -0700
                Re: Snit hitting the glue bag early today Sandman <mr@sandman.net> - 2017-05-02 19:37 +0000
                Re: Snit hitting the glue bag early today Snit <usenet@gallopinginsanity.com> - 2017-05-02 14:12 -0700
                Snit digest 431 / 2017-05-02 Sandman <mr@sandman.net> - 2017-05-02 21:34 +0000
                Re: Snit hitting the glue bag early today DFS <nospam@dfs.com> - 2017-05-02 11:27 -0400
                Re: Snit hitting the glue bag early today Snit <usenet@gallopinginsanity.com> - 2017-05-02 09:49 -0700
                Re: Snit hitting the glue bag early today Sandman <mr@sandman.net> - 2017-05-02 19:36 +0000
                Re: Snit hitting the glue bag early today Snit <usenet@gallopinginsanity.com> - 2017-05-02 14:11 -0700
                Snit digest 430 / 2017-05-02 Sandman <mr@sandman.net> - 2017-05-02 21:33 +0000
                Re: Snit hitting the glue bag early today Sandman <mr@sandman.net> - 2017-05-02 19:23 +0000
                Re: Snit hitting the glue bag early today Snit <usenet@gallopinginsanity.com> - 2017-05-02 14:09 -0700
                Snit digest 429 / 2017-05-02 Sandman <mr@sandman.net> - 2017-05-02 21:32 +0000
                Re: Snit digest 429 / 2017-05-02 chrisv <chrisv@nospam.invalid> - 2017-05-03 07:31 -0500
                Re: Snit digest 429 / 2017-05-02 Silver Slimer <sl@im.er> - 2017-05-03 10:16 -0400
                Re: Snit hitting the glue bag early today Snit <usenet@gallopinginsanity.com> - 2017-05-02 09:48 -0700
                Re: Snit hitting the glue bag early today Sandman <mr@sandman.net> - 2017-05-02 19:33 +0000

csiph-web