Korn shell and awk question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Korn shell and awk question
# 1  
Old 10-26-2007
Korn shell and awk question

I am modifying a Korn shell script in using the Exceed (Solaris 10 environment). My task is to read in a .txt file with dates arranged like this (01-Sep-2006). I am to read each line and take the dates, compare them to a benchmark date and depending on if it is older than the date or the date and newer, produce a result. The problem is that when the first date is read, the condition for the first date follows for the rest of the dates. This is not what I want. I want to have each date tested and produce a different result. Here is what I have:

Code:

#!/bin/ksh
#set -x

#NOW, WE OPEN THE TEXT FILE
exec 3<week_ending_dates1.txt		

while read -u3 LINE 
do			
	day=$(echo $LINE | cut -d'-' -f1)
	month=$(echo $LINE | cut -d'-' -f2)
	year=$(echo $LINE | cut -d'-' -f3)

case $month in
Jan*)
	month=01
	;;
Feb*)
	month=02
	;;
Mar*)
	month=03
	;;
Apr*)
	month=04
	;;
May*)
	month=05
	;;
Jun*)
	month=06
	;;
Jul*)
	month=07
	;;
Aug*)
	month=08
	;;
Sep*)
	month=09
	;;
Oct*)
	month=10
	;;
Nov*)
	month=11
	;;
Dec*)
	month=12
	;;	
esac

# THE DATE IS NOW PUT BACK TOGETHER TO BE COMPARED TO THE BENCHMARK
testdate=$year$month$day  
branchdate="20070814"

if [[ $testdate -lt $branchdate ]]; then
	
more week_ending_dates1.txt | grep Month | awk '{ print "time "$1 \n test1" > "file_"$1}'		
		
else 

more week_ending_dates1.txt | grep Month | awk '{ print "time "$1 \n test2" > "file_"$1}'
		
fi

done	
exec 3<&-

# 2  
Old 10-27-2007
I have modified your script for test purpose (under bash, not ksh) :
Code:
exec 3<week_ending_dates1.txt

while read -u3 LINE
do
        day=$(echo $LINE | cut -d'-' -f1)
        month=$(echo $LINE | cut -d'-' -f2)
        year=$(echo $LINE | cut -d'-' -f3)

        case $month in
        Jan*) month=01 ;;
        Feb*) month=02 ;;
        Mar*) month=03 ;;
        Apr*) month=04 ;;
        May*) month=05 ;;
        Jun*) month=06 ;;
        Jul*) month=07 ;;
        Aug*) month=08 ;;
        Sep*) month=09 ;;
        Oct*) month=10 ;;
        Nov*) month=11 ;;
        Dec*) month=12 ;;
        esac

        # THE DATE IS NOW PUT BACK TOGETHER TO BE COMPARED TO THE BENCHMARK
        testdate=$year$month$day
        branchdate="20070814"

        if [[ $testdate -lt $branchdate ]]; then
                echo "$LINE -> $testdate LOWER THAN $branchdate"
        else
                echo "$LINE -> $testdate GREATER THAN OR EQUAL TO $branchdate"
        fi

done

Test file:
Code:
01-Jan-2000
02-Feb-2007
14-Aug-2007
20-Dec-2008

Output:
Code:
01-Jan-2000 -> 20000101 LOWER THAN 20070814
02-Feb-2007 -> 20070202 LOWER THAN 20070814
14-Aug-2007 -> 20070814 GREATER THAN OR EQUAL TO 20070814
20-Dec-2008 -> 20081220 GREATER THAN OR EQUAL TO 20070814

The logic of the script is good but i don't understand what you want to do with your more|grep|awk command because the awk program in invalid.

Jean-Pierre.
# 3  
Old 10-29-2007
Thank you. I should have mentioned that the awk is to read in the dates and construct another file with the date. For example, if the date was 01-Sep-2007, test the date and then configure a script that reads

Code:
time 01-Sep-2007
do something
do something

So on an so forth for the entire file. I am curious, what do you mean by the "awk" program being invalid?
# 4  
Old 10-29-2007
Quote:
Originally Posted by mastachef
So on an so forth for the entire file. I am curious, what do you mean by the "awk" program being invalid?
At least, a quote is missing:
Code:
$ more week_ending_dates1.txt | grep Month | awk '{ print "time "$1 \n test1" > "file_"$1}'
awk: { print "time "$1 \n test1" > "file_"$1}
awk:                   ^ backslash not last character on line
$

Quote:
Originally Posted by mastachef
Thank you. I should have mentioned that the awk is to read in the dates and construct another file with the date. For example, if the date was 01-Sep-2007, test the date and then configure a script that reads

Code:
time 01-Sep-2007
do something
do something

Can you show us a complete example of your input file and the corresponding required output.

Jean-Pierre.
# 5  
Old 10-29-2007
The input file you created is correct. It has the dates like so

Code:
01-Sep-2006
25-Aug-2007

The file should read each date and then compare each date to the branchdate. Depending on the date, it should produce a file with the date and specific directives. For example, the first date (01-Sep-2006 converted to 20060901) is earlier than the branchdate (20070814). So it should produce a file that reads

Code:
time 01-Sep-2006
element directive_75

The next date (25-Aug-2007 converted to 20070825) should produce

Code:
time 25-Aug-2007
element directive_main

Every date prior to the branchdate should produce a specific out and the same for dates on/after the branch date. This should happen for each date in the .txt file regardless of order.
# 6  
Old 10-29-2007
Where do the elements come from ?

Jean-Pierre.
# 7  
Old 10-31-2007
The elements are later used by Clear Case to load the config_specs. I am trying to produce different config_specs for each date.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

korn shell remove files question

how do you show each filename in a giving directory and delete the specific file in korn script i was thinking using ls rm ? but i cant make it work (0 Replies)
Discussion started by: babuda0059
0 Replies

2. AIX

AIX 4.2 Korn shell and grep question

Ho do I find out the verion of the Kron shell on my client`s system ? There is no one to ask. They are not knowledged enough (hard to believe but yes). Also, on that AIX 4.2, I am trying to figure out how to do a grep using a search patter like below but does not seam to work. The '*' do... (11 Replies)
Discussion started by: Browser_ice
11 Replies

3. UNIX for Dummies Questions & Answers

Korn shell awk use for updating two files

Hi, I have two text files containing records in following format: file1 format is: name1 age1 nickname1 path1 name2 age2 nickname2 path2 file 1 example is: abcd 13 abcd.13 /home/temp/abcd.13 efgh 15 efgh.15 /home/temp/new/efgh.15 (4 Replies)
Discussion started by: alrinno
4 Replies

4. Shell Programming and Scripting

Could someone give me an example of awk accessing array defined in Korn Shell?

As per title and much apprecieated! (2 Replies)
Discussion started by: biglau
2 Replies

5. Shell Programming and Scripting

substr() thru awk Korn Shell Script

Hi, I am new stuff to learn substr() function through awk for writing the Korn shell script. Is there a way to copy from XXXX1234.ABCDEF to XXX1234 file names without changing both data files? I appreciate your time to response this email. Thanks, Steve (4 Replies)
Discussion started by: sbryant
4 Replies

6. Shell Programming and Scripting

korn shell question

Hi all, I am trying to tweak my ksh , i am running V: Version M-11/16/88i I have my Backspace and up/down arrows working using the following code in my ~/.profile file. set -o emacs alias __A=$(print '\020' ) alias __B=$(print '\016' ) alias __C=$(print '\006' ) alias __D=$(print... (4 Replies)
Discussion started by: mich_elle
4 Replies

7. Shell Programming and Scripting

Korn Shell Coprocess Performance Question

I am wracking my brains over this. I am trying to use a Korn Shell script to execute an Oracle PL/SQL procedure, using the Oracle command line interface (sqlplus). The script starts sqlplus in a coprocess, and the two processes communicate using a two-way pipe. The bgnice option is off, so both... (8 Replies)
Discussion started by: Mark Puddephat
8 Replies

8. Shell Programming and Scripting

Korn Shell Loop question

I'm needing help with assigning variables inside a while loop of ksh script. I have an input text file and ksh script below and I'm trying to create a script which will read the input file line by line, assign first and second word to variables and process the variables according to the contents. ... (4 Replies)
Discussion started by: stevefox
4 Replies

9. Shell Programming and Scripting

AWK question in the KORN shell

Hi, I have two files with the following content: gmrd.txt 235649;03;2563;598 291802;00;2563;598 314634;00;235649;598 235649;03;2563;598 393692;00;2563;598 411805;00;2563;598 411805;00;2563;598 235649;03;2563;598 414037;00;2563;598 575200;00;2563;598 70710;00;2563;598... (11 Replies)
Discussion started by: penfold
11 Replies

10. Shell Programming and Scripting

Question about Korn Shell

In Korn Shell, can you use "go to" statements? Would you then put paragraph names with a colon? For example, would you specify "goto para1" and then have the paragraph with the label para1:? I am getting an error message when Idid this. I have my paragraph name 'clsbooks:' and I get... (13 Replies)
Discussion started by: Latha Nair
13 Replies
Login or Register to Ask a Question