scripting question Help

Ask a Question related to UNIX Programming, Design and Development.

  1. #1

    Default Re: scripting question Help

    >>>>> "Matt" == Matt P <m_prorok@hotmail.com> writes:

    Matt> I'm new to UNIX programming, and have been asked to write a script
    Matt> that writes the name and number of lines in a file to a new file. The
    Matt> part I'm having trouble with is the format; the # of lines has to be
    Matt> 12 chars and right- justified. IE ' 238562'. Here's what I have so
    Matt> far:

    Matt> echo $filename `cat $filename | wc -l` >> $new_file

    Matt> Which writes the filename and # of lines. Does anybody have an idea
    Matt> of how to do the formating?

    If your system has printf(1) command, you could

    printf '%s%12d\n' $filename `cat $filename | wc -l` >> $new_file

    If not, try awk or something similar

    --
    Arto V. Viitanen [email]av@cs.uta.fi[/email]
    University of Tampere, Department of Computer and Information Sciences
    Tampere, Finland [url]http://www.cs.uta.fi/~av/[/url]
    Arto V. Viitanen Guest

  2. Similar Questions and Discussions

    1. Sprite Scripting question
      I have a project with a large number of sprites that are generated using makeScriptedSprite in DirectorMX '04. As they appear on the stage, if any...
    2. basic scripting question
      I have a couple of TextInput fields, and when you fill them out and hit a "submit" button, it saves that data as an item in a dataSet component. like...
    3. scripting question
      Does anyone have any idea what kind of script has been used on the buttons on this site to give the bouncing effect?...
    4. Freehand 10 scripting question
      What I would like to find or make is a script that will Import or Place an existing Freehand file into a Freehand 10 template and then export it as...
    5. Scripting Question -- Limiting Form Input
      Hi, See attached code. This will do the trick and hopefully is what your looking for. Regards, Dan. <script> function checkValue(theId) {
  3. #2

    Default Re: scripting question Help

    [email]m_prorok@hotmail.com[/email] (Matt P) writes:
    > I'm new to UNIX programming, and have been asked to write a script
    > that writes the name and number of lines in a file to a new file. The
    > part I'm having trouble with is the format; the # of lines has to be
    > 12 chars and right- justified. IE ' 238562'. Here's what I have
    > so far:
    >
    > echo $filename `cat $filename | wc -l` >> $new_file
    >
    > Which writes the filename and # of lines. Does anybody have an idea
    > of how to do the formating?
    use printf(), which is available at least in ruby and perl.
    ruby example:

    #!/usr/bin/ruby

    ARGV.each {|file|
    len = `wc -l #{file}`
    printf("%s %12i\n",file,len.to_i)
    }

    or, in one line:

    ruby -e 'ARGV.each {|file| len = `wc -l #{file}`; printf("%s %12i\n",file,len.to_i)}'

    For description of the printf() format specifiers, see man printf


    HTH & kind regards
    frank

    --
    Frank Schmitt
    4SC AG phone: +49 89 700763-0
    e-mail: frank DOT schmitt AT 4sc DOT com
    Frank Schmitt Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139