Visit Our UNIX and Linux User Community


Storing the contents of a file in a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Storing the contents of a file in a variable
# 1  
Old 10-28-2009
Storing the contents of a file in a variable

There is a file named file.txt whose contents are:

Code:
+-----------------------------------+-----------+
| Variable_name                     | Value     |
+-----------------------------------+-----------+
| Aborted_clients                   | 0         |
| Aborted_connects                | 25683     |
| Binlog_cache_disk_use          | 0         |
| Binlog_cache_use                 | 0         |
| Bytes_received                    | 30308258  |
| Bytes_sent                         | 519327296 |
| Com_admin_commands          | 0         |
| Com_alter_db                      | 0         |
| Com_alter_table                   | 228       |
| Com_analyze                       | 0         |
| Com_backup_table               | 0         |
| Com_begin                          | 19779     |
| Com_call_procedure              | 0         |
| Com_change_db                   | 10380     |
| Com_change_master             | 0         |
| Com_check                          | 0         |
| Com_checksum                     | 0         |
| Com_commit                        | 15414     |
| Com_create_db                    | 3         |
| Com_create_function             | 0         |
| Com_create_index                 | 0         |
| Com_create_table                 | 114       |
| Com_create_user                  | 0         |
| Com_dealloc_sql                   | 0         |
| Com_delete                         | 9470      |
| Com_delete_multi                  | 0         |
| Com_do                               | 0         |
| Com_drop_db                       | 0         |
| Com_drop_function                | 0         |
| Com_drop_index                    | 0         |
| Com_drop_table                    | 114       |
| Com_drop_user                     | 0         |
| Com_execute_sql                   | 0         |
| Com_flush                            | 4         |
| Com_grant                           | 3         |
| Com_ha_close                       | 0         |
| Com_ha_open                       | 0         |

I want to store the contents of this file to a variable named varname.
I am using the command:
Code:
varname=`cat file.txt`

But when i am running:
Code:
echo $varname

The output is:
Code:
+-----------------------------------+-----------+ | Variable_name | Value | +-----------------------------------+-----------+
| Aborted_clients | 0 | | Aborted_connects | 25682 | | Binlog_cache_disk_use | 0 | | Binlog_cache_use | 0 | | Bytes_received |
 30308028 | | Bytes_sent | 519314110 | | Com_admin_commands | 0 | | Com_alter_db | 0 | | Com_alter_table | 228 | | Com_analyze
 | 0 | | Com_backup_table | 0 | | Com_begin | 19779 | | Com_call_procedure | 0 | | Com_change_db | 10380 | | Com_change_master
 | 0 | | Com_check | 0 | | Com_checksum | 0 | | Com_commit | 15414 | | Com_create_db | 3 | | Com_create_function | 0 | | Com_c
reate_index | 0 | | Com_create_table | 114 | | Com_create_user | 0 | | Com_dealloc_sql | 0 | | Com_delete | 9470 | | Com_delet
e_multi | 0 | | Com_do | 0 | | Com_drop_db | 0 | | Com_drop_function | 0 | | Com_drop_index | 0 | | Com_drop_table | 114 | | C
om_drop_user | 0 | | Com_execute_sql | 0 | | Com_flush | 4 | | Com_grant | 3 | | Com_ha_close | 0 | | Com_ha_open | 0 | | Com_
ha_read | 0 | | Com_help | 0 | | Com_insert | 5446 | | Com_insert_select | 10 | | Com_kill | 0 | | Com_load | 0 | | Com_load_m
aster_data | 0 | | Com_load_master_table | 0 | | Com_lock_tables | 271 | | Com_optimize | 0 | | Com_preload_keys | 0 | | Com_p
repare_sql | 0 | | Com_purge | 0 | | Com_purge_before_date | 0 | | Com_rename_table | 0 | | Com_repair | 0 | | Com_replace | 1
60 | | Com_replace_select | 0 | | Com_reset | 0 | | Com_restore_table | 0 | | Com_revoke | 0 | | Com_revoke_all | 0 | | Com_ro
llback | 7 | | Com_savepoint | 0 | | Com_select | 75393 | | Com_set_option | 25888 | | Com_show_binlog_events | 0 | | Com_show
_binlogs | 38 | | Com_show_charsets | 188 | | Com_show_collations | 188 | | Com_show_column_types | 0 | | Com_show_create_db |
 196 | | Com_show_create_table | 5133 | | Com_show_databases | 237 | | Com_show_errors | 0 | | Com_show_fields | 5244 | | Com_
show_grants | 85 | | Com_show_innodb_status | 0 | | Com_show_keys | 45 | | Com_show_logs | 0 | | Com_show_master_status | 0 |
| Com_show_ndb_status | 0 | | Com_show_new_master | 0 | | Com_show_open_tables | 0 | | Com_show_privileges | 0 | | Com_show_pr
ocesslist | 3 | | Com_show_slave_hosts | 0 | | Com_show_slave_status | 0 | | Com_show_status | 24421 | | Com_show_storage_engi
nes | 0 | | Com_show_tables | 547 | | Com_show_triggers | 5109 | | Com_show_variables | 12637 | | Com_show_warnings | 0 | | Co
m_slave_start | 0 | | Com_slave_stop | 0 | | Com_stmt_close | 0 | | Com_stmt_execute | 0 | | Com_stmt_fetch | 0 | | Com_stmt_p
repare | 0 | | Com_stmt_reset | 0 | | Com_stmt_send_long_data | 0 | | Com_truncate | 0 | | Com_unlock_tables | 309 | | Com_upd
ate | 2901 | | Com_update_multi | 0 | | Com_xa_commit | 0 | | Com_xa_end | 0 | | Com_xa_prepare | 0 | | Com_xa_recover | 0 | |

How can i store the contents of the file in the same format as it it there in the file?
# 2  
Old 10-28-2009
You sould quote the variable:

Code:
printf '%s\n' "$varname"

You could also avoid using cat with most shells:

Code:
varname="$(<filename)"

# 3  
Old 10-28-2009
Code:
You could also avoid using cat with most shells

What is the reason behind that?
# 4  
Old 10-28-2009
Quote:
Originally Posted by proactiveaditya
Code:
echo $varname

You may want to enclose the variable in double quotes ...

Code:
[house@leonov] data=$( cat in.file ); echo "$data"
+-----------------------------------+-----------+
| Variable_name                     | Value     |
+-----------------------------------+-----------+

# 5  
Old 10-28-2009
Quote:
Originally Posted by proactiveaditya
Code:
You could also avoid using cat with most shells

What is the reason behind that?
Here's the reason.
# 6  
Old 10-28-2009
Quote:
Originally Posted by proactiveaditya
Code:
You could also avoid using cat with most shells

What is the reason behind that?
Avoid using an external command because:

- the builtin sould be faster
- you avoid unnecessary forks
# 7  
Old 10-28-2009
Thanks, worked like a charm. Can you explain a bit

---------- Post updated at 11:24 AM ---------- Previous update was at 11:21 AM ----------

OK got it. Thanks everybody for the help.

Previous Thread | Next Thread
Test Your Knowledge in Computers #172
Difficulty: Easy
Red Hat is the largest contributor to the GNOME desktop environment.
True or False?

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 Beginners Questions & Answers

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'... (12 Replies)
Discussion started by: am24
12 Replies

3. 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

4. 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

5. 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

6. 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

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

8. 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

9. 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

10. 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

Featured Tech Videos