If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Assign a value to a variable / parameter

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-18-07, 21:36
gauravjain21 gauravjain21 is offline
Registered User
 
Join Date: Mar 2005
Posts: 41
Assign a value to a variable / parameter

In one directory, I've following files,

Filename:
DOC_CACA_PCHK_20071205.txt
DOC_CACA_PCHK_TRIGGER.trg

I want to to do the following;
1. retrieve CACA from *.trg
2. Pass the above value and look for CACA*.txt
3. Retrieve the date part from CACA*.txt, ie: 20071205

Now, I want a one line command only to handle this as I cannot have any external parameters.
Reply With Quote
  #2 (permalink)  
Old 12-28-07, 16:12
PMASchmed PMASchmed is offline
Registered User
 
Join Date: Jun 2004
Location: Long Island
Posts: 696
Don't have UNIX machine in front of me, and have not scripted for a while, but I think something like:

filename=DOC_CACA_PCHK_TRIGGER.trg
filename2=DOC_CACA_PCHK_20071205.txt
search= `ls ${filename} | cut -f2 -d"_"`
ls ${filename2} | grep ${search} | cut -f3 -d"_"

sorry, did not see the 1 line part before I wrote this.
Reply With Quote
  #3 (permalink)  
Old 12-28-07, 18:18
radoulov radoulov is offline
Registered User
 
Join Date: May 2007
Location: Milano, Italy
Posts: 22
With zsh:

Code:
% ls -l                                                        
total 0
-rw-r--r-- 1 radoulov radoulov 0 2007-12-29 00:10 DOC_CACA_PCHK_20071205.txt
-rw-r--r-- 1 radoulov radoulov 0 2007-12-29 00:10 DOC_CACA_PCHK_TRIGGER.trg
% a=(*trg) a=(*${${(s:_:)a}[2]}*.txt);print ${${(s:_:)a}[4]%.*}
20071205
With bash and ksh93:

Code:
$ ls -l
total 0
-rw-r--r-- 1 radoulov radoulov 0 2007-12-29 00:10 DOC_CACA_PCHK_20071205.txt
-rw-r--r-- 1 radoulov radoulov 0 2007-12-29 00:10 DOC_CACA_PCHK_TRIGGER.trg
$ a=(*trg) set -- ${a//[_.]/ };a=(*$2*.txt);set -- ${a//[_.]/ };printf "%s\n" "$4"
20071205
# for fixed filename format
$ a=(*trg) set -- ${a//[_.]/ };a=(*$2*.txt);printf "%s\n" "${a:14:8}"
20071205
# or
$ a=(*trg) set -- *${a:4:4}*.txt;printf "%s\n" "${a:14:8}"
20071205

Last edited by radoulov; 12-28-07 at 18:28.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On