Topic
6 replies Latest Post - ‏2011-08-12T02:21:01Z by nilo2ohbs
SystemAdmin
SystemAdmin
232 Posts
ACCEPTED ANSWER

Pinned topic facing issue while writing Linux scripting

‏2011-03-15T09:53:34Z |
Hi All,

I am writing one script which needs to pick the files present in a directory. Get the file name, there would be a CountryCode present in each and every file, get the country code values from all the file names and then read another configuration file which has countrycode and DirectoryName mapping. Basically the objective is to transfer the files to a directory based on the country code present in the file name.
I am managed to get all the file names using "awk" but then getting country(which probably requires string manipulation) is there any way to manipulate the file names ?

ls -l $DIR | awk '{print $9}'
Updated on 2011-08-12T02:21:01Z at 2011-08-12T02:21:01Z by nilo2ohbs
  • SystemAdmin
    SystemAdmin
    232 Posts
    ACCEPTED ANSWER

    Re: facing issue while writing Linux scripting

    ‏2011-03-15T12:36:40Z  in response to SystemAdmin
    in short, I have a requirement to read multiple file names, get a variable(Country Code) from each of the FILENAME and then get values associated with those variables from a config file.

    Many thanks in advance for the help!
    • J.Y.
      J.Y.
      2 Posts
      ACCEPTED ANSWER

      Re: facing issue while writing Linux scripting

      ‏2011-07-05T09:18:47Z  in response to SystemAdmin
      awk is very powerful, if all you want is in filename, just use:
      ls -1
      then filter the output with awk, if there is a obvious delimiter, like '-' then you can do
      ls -1 | awk -F '-' '{print ...}'
      And there is gsub() that you can use to extract substrings.
  • MihaiGarbia
    MihaiGarbia
    1 Post
    ACCEPTED ANSWER

    Re: facing issue while writing Linux scripting

    ‏2011-03-16T09:28:03Z  in response to SystemAdmin
    Can you give a few examples of those file names?
  • john.mckown
    john.mckown
    1 Post
    ACCEPTED ANSWER

    Re: facing issue while writing Linux scripting

    ‏2011-03-17T16:47:14Z  in response to SystemAdmin
    I guess you mean that the country code (ISO-3166-1) is in the file name itself. Such as: somefile-us.txt (USA) or somefile-de.txt (Germany). And example of the name would be very helpful. In case it is needed, they can be found here:
    http://www.iso.org/iso/country_codes/iso_3166_code_lists.htm
    Now, how to "do something" with them would depend on which shell you use and how generic you want to be in your coding. That is, are you willing to use BASH-only facilities? Or do you want to be as general as possible so that the script would work on Bourne, Korn, C-shell, or something else? In a general case, I might use the case facility.

    for i in *;do
    case $i in
    *-us.*) echo "USA file";;
    *-de.*) echo "German file";;
    *-fr.*) echo "French file";;
    *-uk.*) echo "British file";;
    *) echo "Some other country's file";;
    esac

    But a more specific answer would require knowing the pattern used for the file names.
    • SystemAdmin
      SystemAdmin
      232 Posts
      ACCEPTED ANSWER

      Re: facing issue while writing Linux scripting

      ‏2011-05-20T07:55:04Z  in response to john.mckown
      You can create a file that contains the country code and directory mapping (e.g. countrycode.txt)

      Then try this:

      while read country_code directory
      do
      mv ${country_code} ${directory}
      done < countrycode.txt
      You may need to add more logic on the move statement as it may accidentally move files that partially correspond to the country code.
  • nilo2ohbs
    nilo2ohbs
    1 Post
    ACCEPTED ANSWER

    Re: facing issue while writing Linux scripting

    ‏2011-08-12T02:21:01Z  in response to SystemAdmin
    Hi,

    I think we're in the same issue.
    I don't know how to do it.
    Anyone can help us?

    Take over a car lease or get out of a car lease easily.