1

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:

BPWS_Instance_1:Linux:x86_64:YES:/hosting/download:BPWS_Linux_64.jar paymon_Instance_1:Linux:i686:YES:/hosting/download:paymon_Linux_32.jar 

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.

2
  • I'd suggest awk with FS = ':' . Commented Feb 5, 2013 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 Commented Feb 5, 2013 at 17:09

1 Answer 1

1

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" . fi 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 
8
  • 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! Commented Feb 6, 2013 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. Commented Feb 6, 2013 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 Commented Feb 6, 2013 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. Commented Feb 6, 2013 at 21:37
  • AWESOMEEEEEEEEEEEEEEEEEE great piece of work sir ... Thanks a ton!!! Thanks a tonnnnnnnnnn for the prompt response! Commented Feb 6, 2013 at 21:44

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.