Create new file using heredoc in Bash - linux

I'm sorry if anyone has already asked the question, I searched for it and have not yet found an answer.
I need to create a new text file using heredoc by only one command line.
What I have tried so far without success, something like this-
cat << "" >> newfile.txt
thanks

Triple quotes denote a herestring:
# append blank line, create if it doesn't exist
cat <<< "" >> newfile.txt
There's no reason to use the herestring. A simpler way to do it would be:
# append blank line, create if it doesn't exist
echo >> newfile.txt
You realize both of those are appending a blank line to the file? If you're just trying to create a completely empty file with size 0, do this instead:
# create empty file, truncate if it already exists
> newfile.txt
That will truncate the file if it already exists. If you just want to ensure a file exists but leave it alone if it already does:
# create empty file, do nothing if it already exists
touch newfile.txt

Related

How can I copy parts of a text file and paste them in a new one

I have a text file containing HTML code of different websites like this one textfile:
and I want to copy the source code one at a time and put them in a different text file because I want to compare it with another text file containing the same source code in order to find out if the website has been updated. Each time I copy the next source code to the new file the old one will be deleted so basically then new textfile must contain only one source code at a time.
I have been able to copy the source code of the first page only but I don't know how to read the file from where I left off in order to copy the next source code.
input="./Desktop/sourcecode0.txt"
while read -r var
do
if [ "$var" != "</html>" ]
then
echo "$var" >> "./Desktop/htmlcode.txt"
continue
elif [ "$var" == "</html>" ]
then
echo "$var" >> "./Desktop/htmlcode.txt"
break
fi
done < "$input"
I would recommend to use rather sed (stream editor) for this, above You can do with:
sed '/<\/html>/q' sample.html
sed '/<\/html>/q' input.html >> htmlcode.txt
What above does sed by it default print all lines and on regexp <\/html> is does q print that line and quit.
Could You provide example what You exactly need "to copy next sourcecode"
If I got you right, you want to split sourcecode0.txt into some file, and each file will contain one <html></html> block.
for this task you can use
split -p '<html>' ~/Desktop/test.txt htmlcode_
that will create files with names htmlcode_aa, htmlcode_ab, htmlcode_ac... the number of files is depend on the number of <html></html> block.
if you want you can add later .txt to each file by calling
find ~/Desktop/htmlcode_a* | xargs -I '{}' mv {} {}.txt

Log File Backup and empty the file instead of deleting in linux

I have a log file which i need to take a backup,
Then empty the file instead of deleting it,
Deleting the file will cause someother script to get triggered,
Hence i should only empty it.
Please suggest me a way?
After you've read from the file you can just overwrite the file with > filename This overwrites the file with nothing. It is also equivalent to cat /dev/null > filename.
similar solutions referenced here
To empty a file you can use truncate -s 0 filename
AFAIK there is no easy way to backup a file and empty it at the same time. I faced a similar problem and what I ended up doing is reading the original file line by line and copy them to a new file while keeping count of line numbers. Then I simply remove that number of lines from the original file. I use this to manually rotate some log files for which standard rotating approaches were not an option.
ORIGINAL_FILE="file.log"
NEW_FILE="$(date +%s).file.log"
unset n
while read line; do echo "$line" >> $NEW_FILE; : $((n++)); done < $ORIGINAL_FILE
if [[ -v n ]]; then
sed -i "1,$n d" $ORIGINAL_FILE
fi

Append data without using cat command

i was wondering weather is it possible to append data in a file without using cat command.
I've considered using sed to append data , but as of my knowledge sed only operates after loading the full data into the memory. please do correct me if i'm wrong on this.
If you want to append data to a file, you can simply use the append I/O-redirection >>. For instance:
echo "first line" > file
echo "next line" >> file
Or you could append an entire file
echo "$(<otherfile)" >> file
This command is however not advisable since it will load the entire file first into memory.
A better way is to use tee:
tee < otherfile >> file
Instead of cat, you can also use echo command to do the same.
And ofcourse, >> operator does it.

run cat command for all the files in the directory given in argument of the script file and out put with the name given as second argument

I run the following code for concatenating files in a directory given as the argument for the script file in bash
for i in $*
do
cat $* > /home/christy/Documents/filetest/catted.txt
done
This produce the error
cat: /home/christy/Documents/filetest/catted.txt: input file is output file
I think there are at least 4 things wrong with your script....
Firstly, your loop will set the value of i to the name of each file in succession, so you would want to actually use i inside your loop, like this:
for i in $*
cat "$i" ....somewhere
done
Secondly, if you use the > redirection, each file will land exactly on top of the previous one, so you should really use the >> redirection will append the current file to the end of the previous one like this
for i in $*
do
cat "$i" >> ...somewhere
done
Thirdly, I think you should use double-quoted "$#" to get all your command-line arguments, rather than plain $*
for i in "$#"
...
Fourthly, you can achieve the exact effect I think you want with this simpler command:
cat "$#" > /home/christy/Documents/filetest/catted.txt
You can't cat a file back onto itself. That's what "input file is output file" means. Because catted.txt shows up in your list of arguments to cat, it is going to try to cat to itself. So, move catted.txt to somewhere other than the source directory.

Dont want to press Ctrl +D to end a file. Any alternative

I want a linux cmd to write the value of a variable into a file. Heres what i have,
x=$(cat /home/kate/Documents/Desktop/New-ACE-Deploy/deploy/ace/deploysetup/ConfFiles/online_ace_stable-m4.5.conf)
echo $x
cd /etc/apache2/sites-enabled
cat > online_ace_stable-m4.5.conf
echo "$x" >> "/etc/apache2/sites-enabled/online_ace_stable-m4.5.conf"
But i have to press Ctrl +D to end. I dont want to do it. Any alternatives.
Remove the cat > online_ace_stable-m4.5.conf line and you should be fine with regards to pressing Ctrl+D. If the file may exist before you run the script, you want to additionally replace >> (appending) on the last line with > (overwriting).
If you really need to explicitly make sure the file exists and is empty, use echo -n > online_ace_stable-m4.5.conf.
Your cat command on the 4th line is incorrect. It is missing a file parameter. Without this parameter is copies stdin to stdout. This goes on until stdin is closed, hence you need to use CTRL-D
In order to fix your problem, change the line it to:
cat online_ace_stable-m4.5.conf
(Note the > is gone)
If you want to create an empty (new) file, use touch instead of cat:
touch online_ace_stable-m4.5.conf
or directly echo to the file:
echo "$x" > "/etc/apache2/sites-enabled/online_ace_stable-m4.5.conf"
as it it not require to first create it and make sure that old contents are not kept. (Note the single > instead of the double >>)

Resources