We're using a ksh script for installing one product.

I've another config file, I'd need to read this configuration file from my main script

Content of the Configuration file:


So now in my main script after reading the configuration file If I've BPWS_Instance_1 ( this is the instance name ) which is running ( YES = Running ) on the Linux OS 64 bit processor ( Linux:x86_64 ) then I'd need to get the BPWS_Linux_64.jar file from the location /hosting/download.

  • I'd suggest awk with FS = ':' . – schaiba Feb 5 '13 at 17:00
  • @schaiba Thanks so much for your promt response! I'd need to read each line first from my main script and then look for these – user31522 Feb 5 '13 at 17:09

Depending on what you want to do with the data, there are two main approaches.

You can parse the data in ksh. Use a loop to read the data line by line with the read builtin, specifying : as the separator. This lets you break the input into columns and do what you want with the columns. My example code copies the specified file from the specified location to the current directory, and does that whenever the instance is running; adjust the loop body to whatever you want to do.

while IFS=: read instance_name os arch running location filename junk; do
  if [ "$running" = "YES" ]; then
    cp "$location/$filename" .
done <Configuration

If you needed to to some text processing on the data, awk would be the tool of choice. Here, you'd have to do additional work to process your data, so it's more complicated than while … read ….

awk -F ':' '
    $4 == "YES" { 
        ENVIRON["location"] = $5;
        ENVIRON["$filename"] = $6;
        system("cp \"$location/$filename\" .");
' <Configuration
  • Awesome .... Great work. Thanks so much for your answer!!! The first piece work real good ... should I be using the same while loop to read the file and then pipe it to AWK ?. Thanks so much sir for the response! – user31522 Feb 6 '13 at 21:20
  • @user31522 The two code snippets do the same thing, you use one or the other. I included both because although the first is best here, there are other fairly similar situations where the second one wins. – Gilles Feb 6 '13 at 21:22
  • Yeah. Thanks for the prompt response! you've put both in a wonderful way so want to get teh second one too. Do not want to miss the chance to get this. Secomd one will be more powerful true ... when it come to more processing on the data Please let me know how it is reading the file and sending it to awk ?. example: filename= Configuration – user31522 Feb 6 '13 at 21:34
  • @user31522 Oh, I forgot that bit. <Configuration, like the other one. It means that the command takes its input from the file Configuration. That's IO redirection. – Gilles Feb 6 '13 at 21:37
  • AWESOMEEEEEEEEEEEEEEEEEE great piece of work sir ... Thanks a ton!!! Thanks a tonnnnnnnnnn for the prompt response! – user31522 Feb 6 '13 at 21:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.