Pulling data from a file to create a new file name

Posted on 2011-10-11
Last Modified: 2013-12-26
I am using the third position of a file name to look up a name in a config file; to use in creating a new file name.

From script -- determined if it is the Dev server or production server and the config file .
hostname=`uname -n`
case $hostname
        DEVSERVER ) EDI_ROOT=/usr/gentran/edidev
                              . ./;;
        PRODSERVER ) EDI_ROOT=/usr/gentran/ediprod
                               . ./;;
        * ) exit 1;;

Taking the third position of the file name (WSC_850_11111_22222_20111010.tsv),  to look up the name in the config file.  Then creating a new file name.  

It is not reading by config file.  Not sure what is incorrect with my sytnax.  Attached is my config file.

Using typeset -A partnername
partnername =([11111]=ABC Company            
      [22222]=XYZ Company)
(File is attached)

 WSC_850*.tsv ) cp $file $archive_dir
                        varpartner=`echo $file | cut -d "_" -f3`
                        echo $varpartner
                        cp $newpartnerfile $edireports;;

Question by:bje
    LVL 37

    Expert Comment

    by:Gerwin Jansen
    Hi bje, this shouldn't be that difficult but I'm not completely understanding what you want exactly.

    - From file name "WSC_850_11111_22222_20111010.tsv" you want the 3rd position, this is "11111", right?
    - Then you want to use the "11111" to look into the config file and find "ABC Company"?
    - And finally you want to use "ABC Company" to create a new file called "ABC Company_20111010.tsv"?

    LVL 37

    Accepted Solution

    Something like this:
    varpartner=`echo WSC_850_11111_22222_20111010.tsv | cut -d"_" -f3`
    name=`grep $varpartner parnername.txt | cut -d"=" -f3`
    newpartnerfile="${name}_`date +'%Y%m%d%H%M%S'`.csv"

    Open in new window

    Note that if you really want "ABC Company" in your filename that the space character in the name could cause issues, you'd have to create or copy the new file like this:
    cp "$newpartnerfile" $edireports

    Open in new window


    Author Closing Comment


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
    Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now