Storing file contents to a variable


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Storing file contents to a variable
# 1  
Old 04-14-2016
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.

Moderator's Comments:
Mod Comment Please post in an appropriate forum. For a regular programming question, don't use
Post Here to Contact Site Administrators and Moderators

Last edited by RudiC; 04-14-2016 at 11:13 AM.. Reason: Moved thread to appropriate forum.
# 2  
Old 04-14-2016
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
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 04-14-2016
Hi Ravinder,

Thanks for your reply. I have tried it using
Code:
while

loop as below but did not get the result.

Code:
     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.

Code:
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
Code:
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.
# 4  
Old 04-14-2016
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
# 5  
Old 04-14-2016
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'.

Code:
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.
# 6  
Old 04-14-2016
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.
Code:
awk '{print $2}'  Input_file

Output will be as follows.
Code:
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
# 7  
Old 04-14-2016
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:
Code:
Input=`cat /path/op.diary`

This User Gave Thanks to cero For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to compare each file in two directores by storing in variable

In the below bash I am trying to read each file from a specific directory into a variable REF or VAL. Then use those variables in an awk to compare each matching file from REF and VAL. The filenames in the REF are different then in the VAL, but have a common id up until the _ I know the awk portion... (15 Replies)
Discussion started by: cmccabe
15 Replies

2. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies

3. Shell Programming and Scripting

Storing multiple file paths in a variable

I am working on a script for Mac OS X that, among many other things, gets a list of all the installed Applications. I am pulling the list from the system_profiler command and formatting it using grep and awk. The problem is that I want to be able to use each result individually later in the script.... (3 Replies)
Discussion started by: cranfordio
3 Replies

4. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

5. Shell Programming and Scripting

storing a value from another file as a variable[solved]

Hi all, im having snags creating a variable which uses commands like cut and grep. In the instance below im simply trying to take a value from another file and assign it to a variable. When i do this it only prints the $a rather than the actual value. I know its simple but does anyone have any... (1 Reply)
Discussion started by: somersetdan
1 Replies

6. Shell Programming and Scripting

Storing lines of a file in a variable

i want to store the output of 'tail -5000 file' to a variable. If i want to access the contents of that variable, it becomes kinda difficult because when the data is stored in the variable, everything is mushed together. you dont know where a line begins or ends. so my question is, how can i... (3 Replies)
Discussion started by: SkySmart
3 Replies

7. Homework & Coursework Questions

How to read contents of a file into variable :(

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have to read the contents of each field of a file creating user accounts. The file will be of format : ... (6 Replies)
Discussion started by: dude_me5
6 Replies

8. Shell Programming and Scripting

How to read contents of a file into variable :(

My file is in this format : username : student information : default shell : student ID Eg : joeb:Joe Bennett:/bin/csh:1234 jerryd:Jerry Daniels:/bin/csh:2345 deaverm: Deaver Michelle:/bin/bash:4356 joseyg:Josey Guerra:/bin/bash:8767 michaelh:Michael Hall:/bin/ksh:1547 I have to... (1 Reply)
Discussion started by: dude_me5
1 Replies

9. Shell Programming and Scripting

Reading from a file and storing it in a variable

Hi folks, I'm using bash and would like to do the following. I would like to read some values from the file and store it in the variable and use it. My file is 1.txt and its contents are VERSION=5.6 UPDATE=4 I would like to read "5.6" and "4" and store it in a variable in shell... (6 Replies)
Discussion started by: scriptfriend
6 Replies

10. Shell Programming and Scripting

Storing the contents of a file in a variable

There is a file named file.txt whose contents are: +-----------------------------------+-----------+ | Variable_name | Value | +-----------------------------------+-----------+ | Aborted_clients | 0 | | Aborted_connects | 25683... (6 Replies)
Discussion started by: proactiveaditya
6 Replies
Login or Register to Ask a Question