Linux Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsOperating SystemsLinux Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 7th, 2007, 11:29 PM
mswartz mswartz is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Posts: 14 mswartz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 35 m 57 sec
Reputation 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

Reply With Quote
  #2  
Old July 8th, 2007, 06:02 PM
mswartz mswartz is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Posts: 14 mswartz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 35 m 57 sec
Reputation 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

Reply With Quote
  #3  
Old July 10th, 2007, 04:22 AM
mswartz mswartz is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Posts: 14 mswartz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 35 m 57 sec
Reputation 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

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsLinux Help > Filtering out strings using a for loop

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap