Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2005
    Posts
    41

    Unanswered: 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.

  2. #2
    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.

  3. #3
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •