#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    14
    Rep Power
    0

    Filtering out strings using a for loop


    I have a file with over 3800 locations. What I'm trying to do is loop through the file and print out every 97th location to another file already created in the same directory as the source file. No particular reason to filter out every 97th location other than I just want to see how it's done. My reasoning for the first line of code is because running >>> 'wc -l locations.txt' returns >>> '3880 locations.txt'. So I want to hack out everything after the 0 in 3880 and just keep the actual line count. When I run the script I get the following error message:

    myScript: line 1: syntax error near unexpected token `|'
    myScript: line 1: `numSites=(wc -l locations.txt | sed 's/ locations.txt//')'

    I've also tried other variations and received the following error message on some of those:

    expr: non-numeric argument

    The above error message has something to do with trying to use wc -l to populate a variable, I'm guessing because the file name isn't numeric.


    Below is the code I wrote. I would appreciate any help. I'm using bash in Centos 4.4. Thanks!

    * * * * * * * * *

    numSites=(wc -l locations.txt | sed 's/ locations.txt//')

    for sites in locations.txt
    do

    # mod division to find every 97th line
    if (expr $numSites % 97 == 0) then
    echo $sites >> newSites.txt
    fi
    done
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    14
    Rep Power
    0
    The above code that I posted doesn't even come close to doing what I want it to do. I came up with the below code. It comes closer to what I want but not exactly. To make it easier to deal with I changed the source file 18 locations, and changed the code to print out every 3rd site. I'll deal with having it append to another file later. I get output, but it prints out >>>

    0
    0
    <every third line is all 18 sites>
    0
    0
    <every third line is all 18 sites>
    . . .
    does the above until EOF

    I'm thinking there will also be an issue with it printing out the correct line since arrays are zero indexed. So if anyone cares to help out that would be great.

    *********************************************

    counter=`wc -l 18sites.txt | sed 's/ 18sites.txt//'`
    array[i]=`cat 18sites.txt`

    for ((i = 1 ; i <= counter ; i++))
    do
    if (expr $i % 3 == 0) then
    echo ${array[@]:$i}
    fi
    done
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    14
    Rep Power
    0
    *** GOT IT!!! ***

    Well..., at least it works on Cygwin. Later today I'll check it out at work on SunOS using bash, and on Centos 4.4 also using bash.


    Yea me!

    *********************************************

    # get the number of lines in the file
    numLines=(`wc -l 18sites.txt | sed 's/ 18sites.txt//'`)

    # load the file into an array, hack off newlines and replace with whitespace
    array=(`cat 18sites.txt | tr '\n' ' '`)

    # because of array zero indexing, decrement the counter by one
    counter=`expr $numLines - 1`

    # print out every 3rd line from the array
    for ((i = 0 ; i <= $counter ; i++))
    do
    # adjust for zero indexing
    tmp=`expr $i + 1`

    # calculate the remainder from mod division
    remainder=`expr $tmp % 3`

    # test for a remainder of zero, print the value if the test is met
    if (test $remainder -eq 0 -a $i -gt 0)
    then
    echo ${array[$i]}
    fi
    done

IMN logo majestic logo threadwatch logo seochat tools logo