Problem with KSH script using scalar variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with KSH script using scalar variable
# 1  
Old 09-18-2010
Problem with KSH script using scalar variable

Hi Guys,

I'm having a hard time bringing out my desired output from my korn shell script. This particular statement from my script its seems not working perl -ne 'print if $_ lt ${date1}' . My complete script as shown below. Please help.

Code:
Code:
#!/usr/bin/ksh

HOME=/export/home/REPORTS/Stats
BIN=${HOME}/bin
OUT=${HOME}/out
date1=`date '+20%y-%m-%d'`
hostname=`hostname`

work1=`cat file.txt | grep -i ' 2 1' | nawk '{print $4}' | perl -ne 'print if $_ lt ${date1}' > ${OUT}/report.txt`

input file:
file.txt
Code:
Jason         2         1               2009-11-21
Richard       7         1               2011-03-29
Mark          2         1               2010-10-02
Michael      14         1               2010-08-23
Nora         15         1               2010-06-23
Beth          2         1               2008-02-06
Nick          2         1               2010-12-09
Danny         2         1               2010-10-27
Dexter       30         1               2010-05-07
Garry         2         1               2011-06-14
Allan         2         1               2010-02-26
Ben          39         1               2010-03-15

Now I'm expecting output like this below.

output file:
report.txt
Code:
2009-11-21
2010-10-02
2008-02-06
2010-02-26

Thanks in advance.


Br,
victorneri

Last edited by Scott; 09-18-2010 at 07:32 AM.. Reason: Code tags
# 2  
Old 09-18-2010
Don't forget to use code tags.

If you need display full year, use this command:

Code:
date '+%Y-%m-%d'

2010-09-18

Last line can be replaced by one awk:

Code:
work1=`nawk -v d=$date1 '$2==2&&$3==1&&$4<d{print $4}' file.txt > ${OUT}/report.txt`

# 3  
Old 09-18-2010
Hi rdcwayx,

Thanks for the reply. But the code you gave is still not working. The statement in your code seems you are comparing ' 2 1 date' versus $date1.

What I want to compare is only the date on $4 having ' 2 1' versus the date today ($date1). And not the entire ' 2 1 date'. Thanks.


Best Regards.

---------- Post updated at 01:11 PM ---------- Previous update was at 12:46 PM ----------

Hi rdcwayx,

Just to let you that is perfectly working now. You just gave me the idea on the date code (date '+%Y-%m-%d') and the nawk -v d=$date1. It works for me.

Thanks mate!

Great to be member here! Smilie Cheers!


Br,
victorneri

---------- Post updated at 01:31 PM ---------- Previous update was at 01:11 PM ----------

I forgot to attach the code.

Code:
work1=`cat file.txt | grep -i ' 2 1' | nawk -v d=$ngayon '$4 <= d {print $4}' > ${OUT}/report.txt`

Thanks gain.

---------- Post updated at 01:46 PM ---------- Previous update was at 01:31 PM ----------

Hi,

Now I want to make it a loop on an array. From my input file, the array will be the CLASS (2, 7, 14, 15, 30, 39) column. Please help me the code on how to write a loop that performs an operation on every element of an array.

Code:
#!/usr/bin/ksh

HOME=/export/home/REPORTS/Stats
BIN=${HOME}/bin
OUT=${HOME}/out
date1=`date '+20%y-%m-%d'`
hostname=`hostname`

work1=`cat file.txt | grep -i ' 2 1' | nawk -v d=$ngayon '$4 <= d {print $4}' > ${OUT}/report.txt`

Input file:

Code:
NAME        CLASS      SEC           DATE         
Jason         2         1         2009-11-21
Richard       7         1         2011-03-29
Mark          2         1         2010-10-02
Michael      14         1         2010-08-23
Nora         15         1         2010-06-23
Beth          2         1         2008-02-06
Nick          2         1         2010-12-09
Danny         2         1         2010-10-27
Dexter       30         1         2010-05-07
Garry         2         1         2011-06-14
Allan         2         1         2010-02-26
Ben          39         1         2010-03-15

Thanks in advance.


Br,
victorneri
# 4  
Old 09-18-2010
First, cat is useless.

Second, back to the nawk command, if you need set CLASS as var, then just set it.

Code:
for class in 2 7 14 15 30 39
do
  work1=`nawk -v d=$date1 -v c=$class '$2==c&&$3==1&&$4<d{print $4}' file.txt > ${OUT}/report.txt`
done

# 5  
Old 09-18-2010
Code:
$ ruby -F"\s+" -ane 'BEGIN{t=Time.now;y=t.year;m="%02d"%t.month;d="%02d"%t.day};print if $F[1]+$F[2]=="21" and $F[3]<"#{y}-#{m}-#{d}" ' file
Jason         2         1               2009-11-21
Beth          2         1               2008-02-06
Allan         2         1               2010-02-26

# 6  
Old 09-18-2010
Quote:
Originally Posted by rdcwayx
First, cat is useless.

Second, back to the nawk command, if you need set CLASS as var, then just set it.

Code:
for class in 2 7 14 15 30 39
do
  work1=`nawk -v d=$date1 -v c=$class '$2==c&&$3==1&&$4<d{print $4}' file.txt > ${OUT}/report.txt`
done

Hi rdcwayx,

I think your code is OK. But as I've said the statement in your code seems you are comparing ' 2 1 date' versus $date1. What I want to compare is only the date on $4 having ' 2 1' versus the date today ($date1). And not the entire ' 2 1 date'. Thanks.


Br,
victorneri

---------- Post updated at 03:27 PM ---------- Previous update was at 03:02 PM ----------

Hi rdcwayx,

Sorry but this is my actual input file. They are in comma separated by the way. I think your code is correct but please re-consider the comma (,) in your code. Thanks again.


Input file:
Code:
NAME,CLASS,SEC,DATE         
Jason,2,1,2009-11-21
Richard,7,1,2011-03-29
Mark,2,1,2010-10-02
Michael,14,1,2010-08-23
Nora,15,1,2010-06-23
Beth,2,1,2008-02-06
Nick,2,1,2010-12-09
Danny,2,1,2010-10-27
Dexter,30,1,2010-05-07
Garry,2,1,2011-06-14
Allan,2,1,2010-02-26
Ben,39,1,2010-03-15

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh script trying to pass a variable to edit a file

I'm trying to create a ksh script that will ask the user for the port number. $PORT1 is the variable I want to use that will contain whatever numbers the user inputs. The script would edit ports.txt file, search and delete "./serv 110.1.0.1.$PORT1 200;=3" . So if the user types 50243 then the... (5 Replies)
Discussion started by: seekryts15
5 Replies

2. Shell Programming and Scripting

Setting a variable in a while loop (.ksh script)

Hello Everyone, I'm still trying to grasp many concepts in .ksh scripting, one of them being variables inside loops. My problem is the following: * I'm trying to set a variable inside a while read loop to reuse it outside of said loop. My lines are the following :... (13 Replies)
Discussion started by: jimmy75_13
13 Replies

3. Shell Programming and Scripting

Problem reading into Variable using KSH

hi all, i create 1 file.txt and inside it contain : file1 file2 file3 file 4 then output that i want in my script: $1=file1 $2=file2 $3=file3 $4=file4 but,when write in ksh script with: while read folder set - $( echo ${folder} ) i=1 while (($i <= $#)) do ... (2 Replies)
Discussion started by: proghack
2 Replies

4. Shell Programming and Scripting

ksh help assigning specific values to variable in script

Hi - Help needed. I have an input file that looks something like this, but with a lot more entries: A Customer1 B 4500 C 8000 A Customer2 B 6422 C 8922 I need to be able to print details for each customer on one line per customer. ie. if I could print these to a file and then cat... (3 Replies)
Discussion started by: frustrated1
3 Replies

5. Shell Programming and Scripting

export variable from ksh script doesn't work

Hi there, in a script I have #!/usr/bin/ksh TEST=hello export TEST Problem is, that the variable doesn't get exported. I also tried typeset -x TEST=hello When I put the two lines in my .profile, the variable is set fine. Whats could be the problem here? (4 Replies)
Discussion started by: doc_symbiosis
4 Replies

6. Shell Programming and Scripting

KSH script eval(?) to set variable

first of all, thanks to all on this board, it has been a huge resource to answer most of my questions! I am stuck on something that should really be simple, and was looking for some help.. I am using KSH on solaris and working on a script to move containers from server to server. Where i am... (4 Replies)
Discussion started by: tksol
4 Replies

7. Shell Programming and Scripting

Why is a variable behaving differently in ksh script.

Guys i have strange behaviour with command output being saved in a variable instead of a tmp file. 1. I suck command output into a variable Sample command output # cleanstats DRIVE INFO: ---------- Drv Type Mount Time Frequency Last Cleaned Comment *** ****... (1 Reply)
Discussion started by: lavascript
1 Replies

8. Shell Programming and Scripting

scalar variable assignment in perl + { operator

When reading over some perl code in a software document, I came across an assignment statement like this $PATH = ${PROJECT}/......./.... In this particular form of scalar variable assignment, what does the curly braces operators do ? Also, what is the benefit in doing scalar assignment this... (3 Replies)
Discussion started by: JamesGoh
3 Replies

9. Shell Programming and Scripting

perl scalar variable in backquoted string

hi I've been searching all over the internet to simply do the following: $tempfile = "/usr/school/tempfile.dat"; $myvar = param('add'); ###add is the variable assigned to a popup menu `ls -l $myvar * >> $tempfile` ###I also tried `ls -l ${myvar}* >>$tempfile` open(ADDLIST,... (6 Replies)
Discussion started by: mehdi9
6 Replies

10. Shell Programming and Scripting

How to pass variable to SQLPLUS in a ksh script?

Hi, I am writing a ksh script which will use sqlplus to run a sql and pass 2 variables as the SQL request. In the ksh script, I have 2 variables which are $min_snap and $max_snap holding 2 different numbers. Inside the same script, I am using SQLPLUS to run an Oracle SQL script,... (6 Replies)
Discussion started by: rwunwla
6 Replies
Login or Register to Ask a Question