Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

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, 22:36
gauravjain21 gauravjain21 is offline
Registered User
 
Join Date: Mar 2005
Posts: 38
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, 17:12
PMASchmed PMASchmed is offline
Registered User
 
Join Date: Jun 2004
Location: Long Island
Posts: 400
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, 19: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 19: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

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