Today (Saturday) We will make some minor tuning adjustments to MySQL.

You may experience 2 up to 10 seconds "glitch time" when we restart MySQL. We expect to make these adjustments around 1AM Eastern Daylight Saving Time (EDT) US.


Bash: Insert in a variable a file


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Bash: Insert in a variable a file

hi all
i have a problem in the bash shell. i'd like insert in a variable a file for example :

i have a file datafine.log in this file there is :
Code:
17/JUN/2019

i want to insert the value of datafine.log in a variable.
Regards
Frncesco

Moderator's Comments:
Mod Comment edit by bakunin: please use CODE-tags for your data as well as code. Thank you. And while you are at it, please don't write in two-word-lines. This is a question in a technical forum, not a poem.

Last edited by bakunin; 4 Weeks Ago at 10:57 AM..
# 2  
Quote:
Originally Posted by Francesco_IT
i have a file datafine.log in this file there is :
Code:
17/JUN/2019

i want to insert the value of datafine.log in a variable.
First, you should notice that funny things ca happen if the content of the file is more than the (probably expected) one line of text. Some caution is advised before you use data in this way therefore.

Aside from these considerations it is quite easy:

Code:
var=$( < /path/to/file )

$( .... ) is a subshell which runs the commands inside the brackets and puts it output on the line, similar to a variable expansion. The command < /path/to/file is a simple redirection which means the output is the content of the file.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 3  
Note that
Code:
var=$( < /path/to/file )

works in ksh, bash, zsh.
The standard shell needs a command like cat
Code:
var=$( cat < /path/to/file )

It is safer to only read one line from the file:
Code:
IFS= read -r var < /path/to/file

And this works in standard-compliant shells.
The IFS= and the -r force a raw read. By default read strips leading and trailing space and continues with the next line if the line ends with a \ character.
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Insert value of env variable in xml file
jakSun8
Hello, I have the following variables set in my env echo $MY_XSD_FILE /home/jak/sample.xsd echo $MY_INTERVAL_VALUE 4 I want to insert them between the xml tags in my xml file cat sample.xml ::::::::::::::: ::::::::::::::: <property name="FILE"></property> :::::::::::::::::::::::...... Shell Programming and Scripting
2
Shell Programming and Scripting
Insert text into file depending on variable
theshams
Hey guys , i have a variable with the contents ... NUMBER=4 and a test file with the contents 1248 1213 1214 1278 1200 3045 3444 2130 I want to execute a script that will produce the following output ( based on NUMBER=4) to be ... create 1248... Shell Programming and Scripting
1
Shell Programming and Scripting
insert line into file using variable
aimmanuel
Hi all, I am trying to insert couple of lines before first occurance of line3 which occuring after line 5. So I identified the line 5 line number in the file. Also redirected the all line3 line number to out.txt. Now I have problem in inserting the line using the variable $rep. Please help me...... Shell Programming and Scripting
2
Shell Programming and Scripting
How do I insert a line in the middle of a file in BASH?
phenom
I have tried sed '/6/a text_to_inserted' file > newfile but this inserts test_to_insert at random places in file and i want it in specific location, which is line 6. can anyone help....... Shell Programming and Scripting
6
Shell Programming and Scripting
insert a variable in the last column of a file
dineshr85
i want to insert a variable in the last column of a file, the columns are separated by "|". I want to insert the variable in everyline of the file .... Shell Programming and Scripting
7
Shell Programming and Scripting

Featured Tech Videos