Register
It is currently Mon Jul 28, 2014 6:34 pm

for loop?


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Sat Aug 07, 2010 3:53 am   

Joined: Thu Oct 09, 2008 3:26 am
Posts: 15
Location: Columbus, OH
Couldn't find this in my bash bible and searching "for loop" wound up with 254 entries. I'd like to take two files "base" and "input" and use "input" to modify "base" as such:

base file
    1001
    1002
    1003
    1004
    1005
    1006
    1007

list file
    1002
    1004
    1005

and wind up with the result:
    1001
    1002 *
    1003
    1004 *
    1005 *
    1006
    1007

And should "list" be reloaded for additional runs with an entry (or more) that has previously been "astrisked" in "base", have it display two asterisks.
Thanks all !!!


Top
 Profile  
 PostPosted: Sat Aug 07, 2010 10:29 am   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
Since you're reading 2 files, I would recommend 2 'while read line' loops instead of a 'for' loop.
Keep in mind that 'while read line' loops are kind of slow, so they're OK with small files, but not with megabytes of data.
Change the basefile.txt and listfile.txt as appropriate.

Code:
#!/bin/bash

while read basenum baseline; do
  while read listnum; do
    if [ "$basenum" == "$listnum" ]; then
      baseline=${baseline}*
    fi
  done < "listfile.txt"
  echo "$basenum $baseline"
done < "basefile.txt"


Top
 Profile  
 PostPosted: Sat Aug 07, 2010 12:35 pm   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
No need for loops:
Code:
sed 's%^%/%;s%$%/s/$/ */%' list | sed -f- base

B-)

Update: If numbers are of variable length, you might need to complicate it a bit:
Code:
sed 's%^%/^%;s%$%($| )/s/$/ */%' list | sed -rf- base

To prevent e.g. 36 in list to asteriskify 364 in base.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP