Storing file contents to a variable

Hi All,

I was trying a shell script. I was unable to store file contents to a variable in the script. I have tried the below but unable to do it.

Input = `cat /path/op.diary`

Input = $(<op.diary)

I am using ksh shell. I want to store the 'op.diary' file contents to the variable 'Input' and i want to use some other commands later on the variable 'Input'.

Can anyone one of you help me on this ?

Thanks in advance.

Hello am24,

Not sure about your complete requirement but storing whole file's content into a single variable doesn't seems to be a good method, in spite of that you could read a file line by line by using while loop or you could use awk , sed to accomplish your requirements, if you have any queries please do let us know complete requirement with sample Input_file and expected output, hope this helps.

Thanks,
R. Singh

1 Like

Hi Ravinder,

Thanks for your reply. I have tried it using

while

loop as below but did not get the result.

     while IFS = read -r line
      do
      echo "$line"
      done < "$file"

My input file looks like below. I am giving a few lines of it. Original file contains 210 lines.

may16 500 30.148 69.852 32.871 35.413 19.372 12.345  0.296  0.000 18.440  0.557  0.051 42.266  9.246  0.000 13.865  0.030 15.248 67.067 32.933 13.212 86.788  7.987 92.013  0.000100.000 37.578 62.422
may16 501  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
may16 502 30.808 69.192 27.597 33.934 20.517 17.952  0.225  0.000 20.105  0.773  0.000 41.955 10.439  0.000 14.266  0.053 12.185 63.017 36.983 13.513 86.487  8.937 91.063  0.073 99.927 41.422 58.578
may16 503 23.417 76.583 18.366 31.827 24.033 25.774  1.421  0.171 15.536  0.448  0.115 23.530 24.581  0.000 19.726  0.058 14.414 60.157 39.843 21.544 78.456 19.704 80.296  0.145 99.855 47.799 52.201
may16 504  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000

After storing the file contents to a variable , I want to use grep command to remove some of the records in field 2 and then i want to append some other file to the resulting file.

When i am asking the user to enter the file manually and read the file with the

read

command, i am able to do it but when i am trying the script to read itself,can not able to do.

Please let me know your thoughts on this.

Thanks in advance.

---------- Post updated at 08:07 AM ---------- Previous update was at 08:02 AM ----------

One more thing i would like you to know that i did not try using awk or sed as i do not know how to use them for this purpose.

Can you please share it by giving one example ?

Thanks in Advance.

Hello am24,

Could you please post sample Input_file and your expected output with all your requirements, I could try to help you with awk , sed etc then.

Thanks,
R. Singh

Hi Ravindhar,

Thanks for your reply. I just want to make the shell script to read the file on its own instead of manually entering input. Below is how my sample file looks like. I want to store it in a variable say 'Input' .

may16 500 30.148 69.852 32.871 35.413 19.372 12.345  0.296  0.000 18.440  0.557  0.051 42.266  9.246  0.000 13.865  0.030 15.248 67.067 32.933 13.212 86.788  7.987 92.013  0.000100.000 37.578 62.422
may16 501  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
may16 502 30.808 69.192 27.597 33.934 20.517 17.952  0.225  0.000 20.105  0.773  0.000 41.955 10.439  0.000 14.266  0.053 12.185 63.017 36.983 13.513 86.487  8.937 91.063  0.073 99.927 41.422 58.578
may16 503 23.417 76.583 18.366 31.827 24.033 25.774  1.421  0.171 15.536  0.448  0.115 23.530 24.581  0.000 19.726  0.058 14.414 60.157 39.843 21.544 78.456 19.704 80.296  0.145 99.855 47.799 52.201
may16 504  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
may16 505  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
may16 506  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
may16 507 21.974 78.026 18.949 32.026 24.177 24.847  1.411  0.060 26.614  0.688  0.072 23.768 18.673  0.088 18.076  0.000 10.551 59.482 40.518 22.226 77.774 12.692 87.308  0.191 99.809 50.330 49.670
may16 508  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
may16 509 30.166 69.834 24.492 34.899 22.264 18.345  0.309  0.032 15.843  0.407  0.042 19.645 12.387  0.148 20.263  0.254 30.670 68.286 31.714 15.311 84.689 14.935 85.065  0.000100.000 42.348 57.652
may16 510  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
may16 511  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
may16 512  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
may16 513 35.127 64.873 20.950 35.699 24.932 18.419  0.367  0.193 26.181  0.451  0.000 29.213 10.682  0.000 16.353  0.000 16.559 66.105 33.895 12.070 87.930 12.100 87.900  0.115 99.885 43.288 56.712
may16 514 30.170 69.830 23.636 34.215 23.150 18.999  0.490  0.077 22.304  0.542  0.023 37.657 10.990  0.148 15.745  0.166 11.858 63.915 36.085 17.738 82.262  7.400 92.600  0.020 99.980 47.803 52.197
may16 515 29.606 70.394 20.281 31.387 23.488 24.844  0.322  0.122 20.758  0.447  0.032 34.009 11.092  0.064 13.470  0.048 19.637 66.862 33.138 16.208 83.792  6.952 93.048  0.093 99.907 51.658 48.342
may16 516 32.779 67.221 23.010 33.483 23.722 19.785  0.642  0.070 13.521  0.500  0.000 34.379 12.732  0.000 20.424  0.120 17.612 67.990 32.010 15.364 84.636 15.279 84.721  0.000100.000 46.035 53.965
may16 517  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
may16 518 26.814 73.186 19.678 34.267 24.457 21.597  0.670  0.144 19.662  0.481  0.053 34.263 13.244  0.028 16.687  0.035 14.732 59.807 40.193 18.835 81.165  9.162 90.838  0.110 99.890 47.205 52.795
may16 519 23.497 76.503 19.951 33.644 24.393 22.012  1.514  0.099 29.253  0.211  0.100 27.666 13.100  0.137 11.779  0.000 16.141 60.288 39.712 18.024 81.976  6.290 93.710  0.000100.000 48.908 51.092

Please let me know if you need any other information.

Thanks in advance.

Hello am24,

You haven't shown the expected output or your aim for writing the script(you have mentioned same thing of storing the Input_file's line into a variable which you told into very first post.). Putting a very wild guess like you wanted to print field 2 of each line, following may help you in same the.

awk '{print $2}'  Input_file

Output will be as follows.

500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
 

Please get back to me in case any queries with complete details of your requirement with samples please.

Thanks,
R. Singh

am24, do you see a syntax error when you use the commands in your first post or what does "unable to do it" mean?
When you asign a value to a variable there are no spaces allowed around the equal sign, no matter where the content of the variable came from.
At least this should work unless you hit some size limit:

Input=`cat /path/op.diary`
1 Like

Well, now we have got the input file. I have to second RavinderSingh13 saying that it's highly improbable you want store that file's contents into a shell variable (as a final goal). What do you want to do with those data? Please show the final output (as a sample) and the logics (conditions, algorithms) that connect both.

Hi Rudi,

Thanks for your reply.

I just want to read the input file by using any code inside the shell script so that the script automatically reads the file and store it in a variable.

As of now i have followed the below in my shell script.

 #!/bin/ksh

(
echo "Enter the sync: \c"
read sync
echo "Enter the input: \c"
read Input1

egrep -iv ''$sync' 513|'$sync' 519|'$sync' 532|'$sync' 540|'$sync' 563|'$sync' 566|'$sync' 632|'$sync' 669|'$sync' 686|'$sync' 789|'$sync' 810|'$sync' 811|'$sync' 855|'$sync' 866' $Input1 > test2

cat test2 CR > op.diaryests.4avg.$sync.total.try

sort -o op.diaryests.4avg.$sync.total.try op.diaryests.4avg.$sync.total.try

exit
 )

I need to read six different files and apply some grep and cat and sort commands. So instead of asking the user to enter six files manually, i want the script to read the file from the path when it executed.

Is there any way to read the input file instead of the way i used in the script ?

Hope i am clear on my specifications.

Thanks in advance.

---------- Post updated at 09:49 AM ---------- Previous update was at 09:44 AM ----------

Hi Ravindhar,

I am sorry, may be i am not clear on my specifications.
Below is the script which i wrote.

#!/bin/ksh

(
echo "Enter the sync: \c"
read sync
echo "Enter the input: \c"
read Input1

egrep -iv ''$sync' 513|'$sync' 519|'$sync' 532|'$sync' 540|'$sync' 563|'$sync' 566|'$sync' 632|'$sync' 669|'$sync' 686|'$sync' 789|'$sync' 810|'$sync' 811|'$sync' 855|'$sync' 866' $Input1 > test2

cat test2 CR > op.diaryests.4avg.$sync.total.try

sort -o op.diaryests.4avg.$sync.total.try op.diaryests.4avg.$sync.total.try

exit
 )

In the above, i am reading the input by asking user to enter manually when running the shell script. Instead of that, i want the shell script to read the Input file from the path and store it in a variable 'Input1' .

By using the while loop or Input1 = `cat /path/filename/` , i am unable to do it. I am getting a message "Input1 not found" .

Like the above , i have to read six different files and execute same grep and cat commands. So i want to put all of the code in one shell script. Instead of asking user manually to enter six times six different input files, i want the script to read it on its own.

Hope i am clear this time.

Please let me know if you need further any more information.

Thanks in advance.

Hello am24,

If I got your requirement exactly, following may help you in same then.

Input1="/tmp/chumma_file"
Input2="/tmp/chumma1_file"
Input3="/tmp/chumma2_file"

You could add your files as above mentioned into your script and let us know how it goes. Also for using while loop you could try that too as follows.

while read line
do
      echo $line  ##### just for an example
done < "$Input1"

Hope this helps you.

Thanks,
R. Singh

1 Like

This is for your information:

The original input file contains values for these 14 markets and I want to make all those values to '0.000' . So I am grepping out the 14 markets mentioned in script(514,519 etc) from the input file and appending the CR file to the resulting file and then sorting the final resulting file.

The CR file contains the '0.000' values for all the 14 market and it looks like below.

may16 513  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
may16 519  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
may16 532  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
may16 540  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
may16 563  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
may16 566  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
may16 632  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
may16 669  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
may16 686  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
may16 789  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
may16 810  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
may16 811  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
may16 855  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000
may16 866  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000  0.000

Sorry, no, you aren't, at least not for me.

You want to store something in a variable. What are you going to do with that variable? Leave it sitting there until you quit the session?

You have six input files. Do you want to loop across those? Do you have the files' names in another file to be read in the loop? I can't see the difference (nor identity) between "input file" in post#1 and in post#9.

Please take a step back, start over and rephrase your spec in plain English, supplying input data (obv. already given), control file, final output (as a sample) and the logics (conditions, algorithms) that connect both.

Hi Ravindhar,

Thanks for suggestions. I will try it out and let you know the results.

---------- Post updated 04-15-16 at 02:06 AM ---------- Previous update was 04-14-16 at 10:04 AM ----------

Hi Ravindhar,

I have tried the code as you suggested and the below is working.

Input1="/tmp/chumma_file"

I also tried with while loop. But it did not work.

while read line
do
      echo $line  ##### just for an example
done < "$Input1"

However i have proceeded with the first Scenario . Thank you so much for your time and suggestions on this :slight_smile:

Hi Cero,

Thanks for your suggestion. I have tried the below code which you suggested but it did not work.

Input1=`cat /path/op.diary`

So I have proceeded with the code which Ravindhar suggested.

Hi Rudi,

Thanks for your time on this. Unfortunately i am not clear on my specifications to you. But thanks a lot for looking into this. As mentioned above i have proceeded with the code which Ravindhar suggested and it worked fine.

Once again thank you all :slight_smile:

Regards,
am24