Sponsored Content
Full Discussion: For loop scripting
Top Forums Shell Programming and Scripting For loop scripting Post 302582863 by balajesuri on Monday 19th of December 2011 12:04:13 AM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 08:20 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy