Results 1 to 4 of 4

Thread: bash script for getting number of numbers per line

  1. #1
    Join Date
    Mar 2008
    Location
    Nottingham
    Beans
    167
    Distro
    Ubuntu

    Question bash script for getting number of numbers per line

    I need a bash script, that reads the line with the fewest number of numbers (not characters) and the largest number of entries into two files.
    the original file will be something like:
    1 34 5 6 7
    23 45 56 67 45 6 5 4
    2 3
    5 6 7
    So in this example i want to read line 2 into an output file A.dat and line 3 into an output file B.dat
    numbers can be all way up to the 10000's and are always separated by a space. The number of lines per file may vary but shouldn't be more than 20 or 30.
    I can read the file, but after that I get stuck. This is what I have got so far:
    PHP Code:

    #!/bin/bash
    var=0;

    while 
    read line
    do
    echo 
    $line;
    var=`
    expr $var + 1`;
    done "myfile"

    echo $var
    alea iacta est

  2. #2
    Join Date
    Mar 2012
    Location
    Russia
    Beans
    104
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: bash script for getting number of numbers per line

    As an example - a script that reads lines from a file with the numbers and put them:
    Code:
    sum = 0; cat total.bytes | while read num ; do sum=`echo $sum+$num" | bc -q` ; done ; echo $sum
    total.bytes:
    Code:
    1962647
    15458
    85368
    2562448
    32110
    3920

  3. #3
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: bash script for getting number of numbers per line

    Quote Originally Posted by abraxas334 View Post
    I need a bash script, that reads the line with the fewest number of numbers (not characters) and the largest number of entries into two files.
    the original file will be something like:
    1 34 5 6 7
    23 45 56 67 45 6 5 4
    2 3
    5 6 7
    So in this example i want to read line 2 into an output file A.dat and line 3 into an output file B.dat
    numbers can be all way up to the 10000's and are always separated by a space. The number of lines per file may vary but shouldn't be more than 20 or 30.
    I can read the file, but after that I get stuck. This is what I have got so far:
    PHP Code:

    #!/bin/bash
    var=0;

    while 
    read line
    do
    echo 
    $line;
    var=`
    expr $var + 1`;
    done "myfile"

    echo $var
    To count the words in a given line:
    Code:
    num=$(echo $line | wc -w)
    or likeliy more efficient, load in array and count array elements:
    Code:
    linearray=($line)
    num=${#linearray[*]}
    I won't write the whole code, but given the above it is easy to store in two pairs of variables the longest line so far and its word count and the shortest one so far and its word count and print them to files at the end (ie no need to maintain a line count/index)...
    Last edited by ofnuts; March 29th, 2012 at 01:51 PM.

  4. #4
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: bash script for getting number of numbers per line

    awk would be perfect for the job
    at first line (NR==1) { initialize min, max variables with NF (number of fields) and minline, maxline with $0 (full line) }
    for each line { test NF against current min, max and if necessary set them to NF and line variable to $0 }
    at the end (END) { print minline > minfile; print maxline > maxfile }

Tags for this Thread

Bookmarks

Posting Permissions

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