Case Stmt - Need Help Urgently


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Case Stmt - Need Help Urgently
# 1  
Old 06-16-2011
Case Stmt - Need Help Urgently

Hello All,
Need help urgently.. i have a scenario where i have two files
1) mireport_20111406.txt
2) PRLIHSP01_8080.2011-06-11-15_26_31

----------
I want a query something similar to this algorithm :-

Code:
Case 
when file_name is like mireport
then  extract_date=14-06-2011
when file_name is like PRLIHSP
then  extract_date=15-06-2011
end

--Where file_name is variable which will extract and store file_name
-- Extract_date i want to retrieve date part from the file_name

I am begginer in Unix... plz help me ..need it urgently for my Project

---------- Post updated at 03:46 AM ---------- Previous update was at 01:49 AM ----------

I meant.. i am beginner in Unix.. Please help me Smilie

Last edited by pludi; 06-16-2011 at 04:10 AM..
# 2  
Old 06-16-2011
You can do something like that :
Code:
extract_date=$( echo ${file_name} | \
                nawk -F '[_.\-]' '
                   /^mireport/ { printf "%s-%s-%s", substr($2,5,2), substr($2,7,2), substr($2,1,4); next }
                   /^PRLIHSP/  { printf "%s-%s-%s", $5, $4, $3; next }
                '
              )

With your examples :
Code:
for file_name in mireport_20111406.txt PRLIHSP01_8080.2011-06-11-15_26_31
do

extract_date=$( echo ${file_name} | \
                nawk -F '[_.\-]' '
                   /^mireport/ { printf "%s-%s-%s", substr($2,5,2), substr($2,7,2), substr($2,1,4); next }
                   /^PRLIHSP/  { printf "%s-%s-%s", $5, $4, $3; next }
                '
              )

print "${file_name} => ${extract_date}"
done

Output:
Code:
mireport_20111406.txt => 14-06-2011
PRLIHSP01_8080.2011-06-11-15_26_31 => 11-06-2011

Jean-Pierre.
This User Gave Thanks to aigles For This Post:
# 3  
Old 06-16-2011
oops -- deleted --

Last edited by ctsgnb; 06-16-2011 at 05:59 AM.. Reason: oops, i missed something ..
# 4  
Old 06-16-2011
Thanks Jeane.. But the 2nd date i was expecting was '15-06-2011' not '11-06-2011' Smilie


Got it Jeane.. Thanks a lot buddy SmilieSmilie I am trying to amend it..Incase i am stuck anywhr..will definately need your help.

Last edited by iamnoone; 06-16-2011 at 06:03 AM..
# 5  
Old 06-16-2011
Code:
extract_date=$( echo ${file_name} | \
                nawk -F '[_.\-]' '
                   /^mireport/ { printf "%s-%s-%s", substr($2,5,2), substr($2,7,2), substr($2,1,4); next }
                   /^PRLIHSP/  { printf "%s-%s-%s", $6, $4, $3; next }
                '
              )

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Linux

How to use a case stmt in a for loop?

How can I merge the move statements with the "FOR" loop to do a move of a file right after it zips it and not wait until all of the files are zipped to move all outisde the for loop. Here is my current code: for file in `ls -rt $svdumpdir/* | grep -v '.gz$' | grep '.gtt$' ` do echo... (8 Replies)
Discussion started by: mrn6430
8 Replies

2. Shell Programming and Scripting

How to use GOTO stmt in Unix scripting?

my code does somthing like this: #!bin/ksh sqlplus / | While read id do temp=`echo $id` i = i+1 done j=0 while do --connecting to sql and executing a Stored proc for 1st id --checking for the status status = $? if error --need to... (1 Reply)
Discussion started by: RP09
1 Replies

3. Shell Programming and Scripting

Problem comparing String using IF stmt

Hi frnds Im facing an issues while trying to compare string using IF stmt, my code is: chkMsgName=`Service Fee Detail` if then if then if then echo "Valid File Ready for processing" fi fi ... (5 Replies)
Discussion started by: balesh
5 Replies

4. Shell Programming and Scripting

Help with Dates and SQl stmt?

hi All Please explain the below statement in RED?What does this mean? perl /HDS/common/operations/Quality_Team/Nirvana/WEEKLY_OOPS/sql_dump.pl --sql "select * from WEEKLY_REPORT order by test_case, type" --username=ddb_qa --password=ddb_qa123 --sid=pldeldb --output... (1 Reply)
Discussion started by: SVS_2017
1 Replies

5. Shell Programming and Scripting

if stmt argument expected error

CT=0 while read LINE do # Check to see if the LINE is non-empty, and has a <td> tag in it. if then # Increase the TD counter by 1 CT=`echo "$CT+1"` fi done <test.htmthrows this error: ksh: test: argument expected test.htm <tr> <td>text</td... (4 Replies)
Discussion started by: dba_frog
4 Replies

6. Shell Programming and Scripting

I want to get the Unix variable value in the sql stmt

Hi All I have a requirement where in I am stuck. There is a shell script that is being developed by me. It consist of the sql stmt also. I need to export a variable called HOMEPAGE with a value say www.abc.com. and then use this $HOMEPAGE variable in the sql stmt. My ultimate aim is to fetch all... (1 Reply)
Discussion started by: amitsinha
1 Replies

7. Shell Programming and Scripting

Help with awk stmt

Hi All, I have a log file as: cat error.log.tmp1 2010-07-06 23:18:34 Connection Available to xyztestftp.abc.com.my 2010-07-06 23:20:33 Connection Available to xyztestftp.abc.com.my ERROR FTP LOGIN Now I am reading this complete log file if no single occurrence of word "ERROR" (this word... (5 Replies)
Discussion started by: ss_ss
5 Replies

8. Programming

Function Returning Value w/o return stmt

I am working on a C/Unix application from last 2 years which communicates with other systems using proprietary format of my client. We have a function written in C which returns integer, which is response from other system to the request message initiated by my system. This return value is then... (1 Reply)
Discussion started by: dpmore
1 Replies

9. UNIX for Dummies Questions & Answers

Please explain the stmt

hi, Please explain the below stmt. P=1234 var1=:/a/b/c/file.dat printf "%s %s\n" "$" "${var1#?}" Output is: 1234 /a/b/c/file.dat What is #? in printf stmt? by using that the first character( : ) of "var1" variable is not displayed in output. How is that? please explain.. ... (1 Reply)
Discussion started by: srilaxmi
1 Replies
Login or Register to Ask a Question