How can I write variables to same line of a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can I write variables to same line of a file?
# 1  
Old 01-07-2015
How can I write variables to same line of a file?

I am trying to keep variables in a file.

if I have all variables at the same time, I can write them all like below.
Code:
echo $var1","$var2","$var3

But, these variables are being calculated at different times then they are lost
so I want to keep them in a file seperated by "," .
Code:
echo $var1 >> log_file
.
.
.
.
echo ","$var2 >> log_file
.
.
.

is there an option of echo in order to write the variables without creating a new line in the log file.

thanks in advance

Last edited by radoulov; 01-07-2015 at 12:24 PM..
# 2  
Old 01-07-2015
Hello snr_silencer,

You can keep them in a single variable and then finally you can print them as follows.
Code:
VAR2=$VAR1","$VAR2
VAR3=$VAR2","$VAR3
echo $VAR3 > Input_file

Hope this helps, letme know if you have any queries.

Thanks,
R. Singh
# 3  
Old 01-07-2015
Code:
echo -n "${var1}," >> log_file
.
.
echo -n "${var2}," >> log_file
.
.

or

printf "${var1}," >> log_file
.
.
printf "${var2}," >> log_file

# 4  
Old 01-07-2015
thanks for your quick answers
-n worked

---------- Post updated at 10:50 AM ---------- Previous update was at 10:36 AM ----------

ps: -n option doesnt work in ksh, only works for bash shell
# 5  
Old 01-07-2015
That's when you use the built-in echo. Check if there exists /bin/echo and see its man page.
# 6  
Old 01-07-2015
The behavior of echo varies considerably from system to system, shell to shell, and with various environment variable settings when there are any backslash (\) characters in any of the arguments and when the 1st character of the 1st argument is a minus sign (-) character. Using printf instead of echo in these conditions will prevent lots of headaches later if you ever move to a different environment or if some other user with a different shell or environment variables tries to use your code.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search pattern and write line into another file

Hi, I have a file which contains the below details.. My requirement is to fetch all the lines which are starting with "ABC_XY_" into 1 file and rest of the lines (not starting with "ABC_XY_") into another file. Could you please help with what command needs to be used? file1.txt ----------... (12 Replies)
Discussion started by: satyaatcgi
12 Replies

2. Shell Programming and Scripting

write specific line number in file

dear all, i need your advice i have sample script like this: testing.sh for i in {1..10} do echo testing $i done but i forgot create "#!/bin/bash" in above "for" so i want output will like this testing.sh #!/bin/bash for i in {1..10} do echo testing $i done (2 Replies)
Discussion started by: zvtral
2 Replies

3. Shell Programming and Scripting

how read specific line in a file and write it in a new text file?

I have list of files in a directory 'dir'. Each file is of type HTML. I need to read each file and get the string which starts with 'http' and write them in a new text file. How can i do this shell scripting? file1.html <head> <url>http://www.google.com</url> </head> file2.html <head>... (6 Replies)
Discussion started by: vel4ever
6 Replies

4. Shell Programming and Scripting

Write string variables to file

I have the following: #! /bin/bash foo="bar" this="that" vars="foovar=$foo\n\ thisvar=$this\n" I want to write the following to a file: foovar="bar" thisvar="that" Then in another script, I pull this file, and loop through it: while read line; do eval $line done <... (3 Replies)
Discussion started by: Validatorian
3 Replies

5. Programming

How to write a new line to the end of the file in Perl?

i am very new to Perl. i am using Ubuntu. i have a string call $string that contains following words "new line". i also have a data file as follows. djfibjbet etitrbjijbtr rrge rgjierjegjeri jerijg kijij jijij i want to write my new line to my data file as follows. djfibjbet... (3 Replies)
Discussion started by: usustarr
3 Replies

6. Shell Programming and Scripting

Lookup two values per line (from a second file) and write the smaller value to another file

Hello Unix Gurus, Please let me know if this is hard to understand and I apologize for my inability to explain better. I have a file "Foo" with the following structure CHR_A BP_A SNP_A CHR_B BP_B SNP_B R2 1 ... (3 Replies)
Discussion started by: genehunter
3 Replies

7. Shell Programming and Scripting

Write in a file with pipe also in same line

hi, i want to write in a file the output of one command and pile also the same output like ls -lrt > some_file | wc -l (9 Replies)
Discussion started by: narang.mohit
9 Replies

8. UNIX for Dummies Questions & Answers

write new line at the beginning of an existing file

I was trying to find out the easiest way to write new line to the beginning of an exisiting file. I am using KSH. (5 Replies)
Discussion started by: sailussr
5 Replies

9. Shell Programming and Scripting

Re-write first line of a file before printing

Morning All, Quite a simple one this, I hope. What I want to do is to re-write the first line of a file before it's sent to print. The line will be blank initially, and I want to insert some text. The operation can either be done on the file itself (modifying the file on disk), OR in a... (2 Replies)
Discussion started by: alexop
2 Replies

10. Shell Programming and Scripting

Write system variables into file

Hi everyone, I was told, i my job, to do a script that creates the backup of all the files that are important to us. So i created the script, put it in the crontab and it works great. Now what i want is to write to a file what directories have being copied with date and time. How can i... (3 Replies)
Discussion started by: jorge.ferreira
3 Replies
Login or Register to Ask a Question