How to write end of file with new line in linux? - linux

I want to write, a script output into logfile at the end with new line..
with this command, it replace the file.
echo "hai" > /tmp/syslog.txt
but I need add "hai" with existing file content at the end with new line..
thanks in advance..

just simple like this:
echo "hai" >> /tmp/syslog.txt
:)

Using option \n – New line with backspace interpreter -e treats new line from where it is used.
echo -e "\nHai" >> /tmp/syslog.txt

Related

How to echo command output to end of existing line in Linux?

I am trying to automate the building of a config file and most of what I need can do done via a straight echo 'text here' >> myfile.conf
What I am having trouble with is appending the output of a command to the last line in the file.
My last working command is echo 'masternodeprivatekey=' >> myfile.conf
My next command is ./wallet-cli masternode genkey &>> myfile.conf
I need to have the masternode key generated to be included on the same line as masternodeprivatekey= so that the line will end up as this -
masternodeprivatekey=abcd13def456ghi789
Can this be done?
I have seen ppl suggest adding /c to the end of the echo command or -n to the end of the line however these don't work, they just add those characters to the end and still post the output of ./wallet-cli masternode genkey &>> myfile.conf on the next line.
Is it possible to do what I want? I'm pretty sure I could echo the output to two different temp files and then combine them, but is there an easier way?
You can use echo with option -n , it do not output the trailing newline
echo -n 'masternodeprivatekey=' >> myfile.conf
In sed, you can adress the last line with $
echo 'masternodeprivatekey=' >> myfile.conf
sed -i -r "$ s/(.*)/\1$(($RANDOM%100000))/" myfile.con
Check, whether your sed has -i and -r parameters. This worked with GNU-sed.
Since I don't know ./wallet-cli, I used something else in $(...) instead, to perform a command which generates output. So for you, it might be
sed -i -r "$ s/(.*)/\1$(./wallet-cli masternode genkey)" myfile.con
Since you seem to know before, that you will add something after the equal sign, why don't you do:
echo "masternodeprivatekey=$(./wallet-cli masternode genkey)" >> myfile.con
As per Mark Plotnick's suggestion, the use of printf instead of echo works perfectly.

How to add lines to end of file on Linux

I want to add the following 2 lines:
VNCSERVERS="1:root"
VNCSERVERARGS[1]="-geometry 1600x1200"
to the end of the file vncservers found at the directory /etc/sysconfig/.
How can I do this?
The easiest way is to redirect the output of the echo by >>:
echo 'VNCSERVERS="1:root"' >> /etc/sysconfig/configfile
echo 'VNCSERVERARGS[1]="-geometry 1600x1200"' >> /etc/sysconfig/configfile

End of line in shell variables

So I have testfile which contains
Line one
Another line
and this is the third line
My script reads this file, does some stuff and I end up with a variable that should contain it. Kind of doing
filevar=$(cat testfile)
(the important thing here is that I cannot access the file directly).
I'll be using the contents of that variable to generate an HTML code and one things I have to do is to add <br> to the end of each line. The problem is, there doesnt seem to any EOLs in my var:
echo $filevar
Line one Another line and this is the third line
How do I read the file properly to keep the EOLs? Once I have that I can simply sed s/$/<br>/g, but till then...
thanks!
how about changing IFS?
#!/bin/bash
IFS=""
filevar=$(cat test)
echo $filevar
this will output:
Line one
Another line
and this is the third line
I can't understand why do you need to read the file into the variable. Why don't you simply do this:
sed 's|$|<br/>|' testfile
UPDATE:
If you really want to get the EOL back in your variable. Try this (notice the quotes):
echo "$filevar"
But I still can't understand, why you can cat the file but not access the file
As a solution, I would suggest the following script:
while read LINE
do
echo ${LINE} '<br />' # Implement your core logic here.
done < testfile
Instead of doing echo $filevar do echo "$filevar" (note the double quotes). This will send a single argument to echo and then you can pipe this to sed.
With sed, this will be treated as 3 lines, so you do not need the g option. This works with me (bash and cygwin):
echo "$filevar" | sed 's/$/<br>/'
You need to set the IFS variable to contain just a newline, and then reference the filevar variable without quotes.
$ filevar='Line one
Another line
and this is the third line'
$ for word in $filevar; do echo "$word<br>"; done
Line<br>
one<br>
Another<br>
line<br>
and<br>
this<br>
is<br>
the<br>
third<br>
line<br>
$ for word in "$filevar"; do echo "$word<br>"; done
Line one
Another line
and this is the third line<br>
$ (IFS=$'\n'; for word in $filevar; do echo "$word<br>"; done)
Line one<br>
Another line<br>
and this is the third line<br>

How to copy the first few lines of a giant file, and add a line of text at the end of it using some Linux commands?

How do I copy the first few lines of a giant file and add a line of text at the end of it, using some Linux commands?
The head command can get the first n lines. Variations are:
head -7 file
head -n 7 file
head -7l file
which will get the first 7 lines of the file called "file". The command to use depends on your version of head. Linux will work with the first one.
To append lines to the end of the same file, use:
echo 'first line to add' >> file
echo 'second line to add' >> file
echo 'third line to add' >> file
or:
echo 'first line to add
second line to add
third line to add' >> file
to do it in one hit.
So, tying these two ideas together, if you wanted to get the first 10 lines of the input.txt file to output.txt and append a line with five "=" characters, you could use something like:
( head -10 input.txt ; echo '=====' ) > output.txt
In this case, we do both operations in a sub-shell so as to consolidate the output streams into one, which is then used to create or overwrite the output file.
I am assuming what you are trying to achieve is to insert a line after the first few lines of of a textfile.
head -n10 file.txt >> newfile.txt
echo "your line >> newfile.txt
tail -n +10 file.txt >> newfile.txt
If you don't want to rest of the lines from the file, just skip the tail part.
First few lines: man head.
Append lines: use the >> operator (?) in Bash:
echo 'This goes at the end of the file' >> file
sed -n '1,10p' filename > newfile
echo 'This goes at the end of the file' >> newfile

Use bash to read a file and then execute a command from the words extracted

FILE:
hello
world
I would like to use a scripting language (BASH) to execute a command that reads each WORD in the FILE above and then plugs it into a command.
It then loops to the next word in the list (each word on new line).
It stops when it reaches the end of the FILE.
Progression would be similar to this:
Read first WORD from FILE above
Plug word into command
command WORD > WORD
which will output it to a text file; with word as the name of the file.
Repeat this process, but with next to nth WORD (each on a new line).
Terminate process upon reaching the end of FILE above.
Result of BASH command on FILE above:
hello:
RESULT OF COMMAND UPON WORD hello
world:
RESULT OF COMMAND UPON WORD world
You can use the "for" loop to do this. something like..
for WORD in `cat FILE`
do
echo $WORD
command $WORD > $WORD
done
normally i would ask what have you tried.
while read -r line
do
command ${line} > ${line}.txt
done< "file"
IFS=$'\n';for line in `cat FILEPATH`; do command ${line} > ${line}; done

Resources