For loop scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting For loop scripting
# 1  
Old 12-18-2011
For loop scripting

Hi there again,
I need some help in figuring out how to execute a for loop command to come up with my desired output. I have individual monthly files and a single file containing a list of location data for 1 year. I want to do a loop where for each monthly file say Jan.hdf, if will read the location values corresponding to month 1.

Code:
input files:
Monthly files: Jan.hdf ..Dec.hdf
location.dat containing the ff. information:
lon lat month
123.5 12.4 1
124.5 12.3 1
125.5 12.2 2
126.5 12.1 2
127.5 12.0 2... and so on

thanks much. i hope u can help me out resolve this.

cheers, irene
# 2  
Old 12-19-2011
What output do you want? You've shown the input? -- the structure of the files you want to read?
sort of pseudo-code is, it assumes the on hdf files in the current directory are jan .. dec.hdf----:
Code:
awk ' [awk script to give you your output] '  *.hdf > location.dat

There is no need for a loop based on what you've said so far.

If the input file structure matches the output file you can combine all files this way:
Code:
cat *.hdf > location.dat

# 3  
Old 12-19-2011
hi, this the sample code i want to have in concept.

Code:
files ="$(ls *.hdf)"
for i in $files; do
    read lines of location.dat
    plot $i;    
    overlay location points corresponding to similar month
done

Sorry, i'm very poor in explaining. basically, i would like to have a visual output where only location points from january (column 3 of locations.dat=1) will be overlaid on my monthly plot for january (file 1) and so on. thanks much
# 4  
Old 12-19-2011
Is this what you're looking for?

Code:
[root@hostname test]# ls -rt *.hdf
Jan.hdf  Feb.hdf

Code:
[root@hostname test]# cat location.dat
lon lat month
123.5 12.4 1
124.5 12.3 1
125.5 12.2 2
126.5 12.1 2
127.5 12.0 2

bash code:
  1. #! /bin/bash
  2.  
  3. mnths=( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec )
  4.  
  5. for files in `ls -rt *.hdf`
  6. do
  7.     echo "lon lat for the month of $files"
  8.     mt=`basename $files .hdf`
  9.     i=0
  10.     for m in ${mnths[@]}
  11.     do
  12.         i=$(($i + 1))
  13.         if [ "$mt" == "$m" ]
  14.         then
  15.             mt=$i
  16.             break
  17.         fi
  18.     done
  19.     while read LINE
  20.     do
  21.         [[ `echo $LINE | cut -d" " -f1` == "lon" ]] && continue
  22.         [ `echo $LINE | cut -d" " -f3` -eq $mt ] && echo $LINE | cut -d" " -f1-2
  23.     done < location.dat
  24. done

Code:
[root@hostname test]# ./test.sh
lon lat for the month of Jan.hdf
123.5 12.4
124.5 12.3
lon lat for the month of Feb.hdf
125.5 12.2
126.5 12.1
127.5 12.0

---------- Post updated at 10:34 ---------- Previous update was at 10:07 ----------

Slightly quicker in Perl.

perl code:
  1. #! /usr/bin/perl -w
  2. use strict;
  3. my ($file, $m);
  4. my %months = (    "Jan" => 1, "Feb" => 2, "Mar" => 3, "Apr" => 4,  "May" => 5,  "Jun" => 6,
  5.                   "Jul" => 7, "Aug" => 8, "Sep" => 9, "Oct" => 10, "Nov" => 11, "Dec" => 12);
  6.  
  7. for $file (glob "*.hdf") {
  8.     print "lon lat for $file\n";
  9.     $file =~ s/\.hdf//g;
  10.     for (keys %months) {
  11.         if ($file eq $_) { $m = $months{$_} }
  12.     }
  13.     open I, "< location.dat";
  14.     for (<I>) {
  15.         if (/^lon/) { next }
  16.         if (/$m$/) { print $_ }
  17.     }
  18.     close I;
  19. }

Last edited by balajesuri; 12-19-2011 at 12:43 AM..
# 5  
Old 12-19-2011
hi thanks for the reply. If i want to save the output file to individual file name from your code, how will i do that? I think i can start from it. this is my messy code supposedly:

Code:
#!/bin/bash

files="$(ls *.hdf)"

for f in $files; do
   for line in location.dat; do
      grdimage $f $range -Csst.cpt -JM$scale -P -B10g5 -K -O >> "$f".ps (this will produce an image map for each month)
      psxy $line $range -JM$scale -B10g5 -Sc0.05 -Gblack -P -K -O "$f".ps (this will plot location points on the output image from earlier command-
this is where i need to tell the program that it will only plot location points from the list for the given month)
    done 
done

thanks much.
# 6  
Old 12-19-2011
Changes from the previous bash script are highlighted in blue. This way, you can get separate files 'Jan.out', 'Feb.out'... each containing information about that particular month. Manipulate the code to suit your requirement.
Code:
#! /bin/bash

mnths=( Jan Feb Mar Apr Jun Jul Aug Sep Oct Nov Dec )

for files in `ls -rt *.hdf`
do
    mt=`basename $files .hdf`
    echo "lon lat for the month of $files" >> $mt.out
    i=0
    for m in ${mnths[@]}
    do
        i=$(($i + 1))
        if [ "$mt" == "$m" ]
        then
            mt_num=$i
            break
        fi
    done
    while read LINE
    do
        [[ `echo $LINE | cut -d" " -f1` == "lon" ]] && continue
        [ `echo $LINE | cut -d" " -f3` -eq $mt_num ] && echo $LINE | cut -d" " -f1-2 >> $mt.out
    done < location.dat
done

This User Gave Thanks to balajesuri For This Post:
# 7  
Old 12-19-2011
thanks much,Smilie

---------- Post updated at 03:30 PM ---------- Previous update was at 03:11 PM ----------

hi again, i'm thinking of just working on my location.dat file. if supposed i just want loop inside the lines of the .dat file and print lon, lat values using awk for specific month, is this possible and whats the appropriate code for this? thanks much. i think this would be much simple for me.

output file:
Code:
lon lat for jan > month1
lon lat for feb > month2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Scripting Question (For Loop)

Hello, I am practicing creating loops in bash scripting. I am trying to understand the different uses of the for loop. I have a script I made below with some help. #/bin/bash #This script will take the extension of a file in ls command and echo a certain statement based on what type of... (6 Replies)
Discussion started by: shah9250
6 Replies

2. Shell Programming and Scripting

How to get the for loop output from a remote server in UNIX shell scripting?

Hi, I am using ksh , when i try to use for loop i am getting the expected output. $for variable in $(ps -fu user | grep -i something/ | grep -i something | grep -v grep | awk '{print $2}');do > grep $variable /tmp/some_path/*/* > done when tried the below to remote server, getting... (4 Replies)
Discussion started by: karthikram
4 Replies

3. Shell Programming and Scripting

Expect Scripting Loop Argument Desperately Needed!

I am trying to create an Expect script that does the following: 1) Telnets to an IP address and logs in with user ID and Password 2) Issue a CLI command to the server that will output data of which I am particularly interested in a DS1 clock 'Slips' value. I want to be able to keep issuing... (0 Replies)
Discussion started by: dwightlaidler
0 Replies

4. Homework & Coursework Questions

shell scripting while loop lab 15 help

hi.. this is shell scripting lab15.sh i dont understand this lab i am at the screen shot part. i was wondering if someone can take a quick look at this lab i have linked the doc below. i dont know where to start i have did the Required Errorlevels Errorlevel # but dont... (1 Reply)
Discussion started by: beerpong1
1 Replies

5. Shell Programming and Scripting

while loop shell scripting help

hi i was wondering if someone can help me with a while loop..i have been looking at this for hours and dont no wut to do.. i have to make a menu style.. to have a beeter understanding i have linked a photo at the bottom... http://www.mypicx.com/uploadimg/772204432_08022011_1.pngand then ... (1 Reply)
Discussion started by: beerpong1
1 Replies

6. Shell Programming and Scripting

Clarification on if loop in Shell scripting

Hi, I'm using Ksh and I'm seeing some of code in my programme as given below. Could you please let me know whats is this meeaing ? (I'm new to this unix) grep "1034" /u/kkk/bin/temp5.lst|cut -c1-2 >/u/kkk/bin/temp6.lst if then echo "" ... (2 Replies)
Discussion started by: shyamu544
2 Replies

7. Shell Programming and Scripting

while loop in shell scripting

Hi, I have a doubt in usage of while loop in Shell script as Iam new to this. My requirement is that,I have two different directories containing some files which move files to other folder after parsing is done. In my script i wanted to add a while loop which checks for the count in these two... (5 Replies)
Discussion started by: jyothi_wipro
5 Replies

8. UNIX for Dummies Questions & Answers

Loop till you find a string in a fine <-- Need Help New to Unix Scripting

Guys - I am new to Unix scripting and am in need for a script that does the following. I have bits and pieces created and tested but i am just having a little difficult time getting it all together. - Loop through till it finds a string in a specific file. Any help is greatly appreciated. ... (1 Reply)
Discussion started by: mrehman
1 Replies

9. Solaris

Problem in for loop of shell scripting in solaris

Hi below is my script for((i=0;i<=$TOTAL;i++)) do echo "IP's created are $s1.$s2.$s3.$s4" s4=`expr $s4 + 1` done where s1,2,3,4 are input varibles below error occurs while running the script syntax error at lin 11: '(' unexpected ... (12 Replies)
Discussion started by: krevathi1912
12 Replies

10. Shell Programming and Scripting

new to shell scripting: whats wrong with my if loop

#!/bin/bash for file in $HOME/*; do if ; then rm -i $file > /dev/null echo "$?" echo "$file has been deleted" fi done Been trying to learn shell scripting for a week or so now, when i run the script it doesnt display an error message, seems like it runs fine, however it doesnt delete... (10 Replies)
Discussion started by: stride6
10 Replies
Login or Register to Ask a Question