Storing file contents to a variable


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

Code:
 #!/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.

Code:
#!/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.
# 10  
Old 04-14-2016
Hello am24,

If I got your requirement exactly, following may help you in same then.
Code:
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.
Code:
while read line
do
      echo $line  ##### just for an example
done < "$Input1"

Hope this helps you.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 11  
Old 04-14-2016
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.

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

# 12  
Old 04-14-2016
Quote:
Originally Posted by am24
.
.
.
Hope i am clear on my specifications.
.
.
.
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.
# 13  
Old 04-15-2016
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.

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

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 :-)

Regards,
am24
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