Echo commented lines into a properties txt file - linux

Is there any way we can redirect commented data into txt files using echo command?
For eg:
echo #abcd1
abcd2
abcd3 >> newFile.txt
I need to comment out the entire block that I am redirecting into the text file. Hence, after executing echo, the newFile.txt should look like:
#abcd1
#abcd2
#abcd3

Escape it.
$ echo \#foo > foo
$ cat foo
#foo

Related

how to create a file with line of text in Linux

Create a file bash/alx with these two lines inside: #!/bin/bash and echo "ALX" in linux
I have tried this:
touch bash/alx
touch '#!/bin/bash'
touch is a command that creates or updates the timestamp on a file.
echo foo outputs foo to stdout.
If you use output redirection, echo foo > bar puts foo into the file bar.
Combining these will give you your solution.
touch /path/t/myFileName
echo "text for file" > /path/to/myFileName
This page has some good information.
To create a file in Linux using touch,the syntax is:
touch /path/to/your/file
touch alx.txt # E.g
You can echo something in a file using (by default echo output to stdout):
echo content > file
# or
echo content >> file
The former override the file content if it already exist or create it while the latter will append your content to the file if it exists or create it too.
Thus you can use echo directly without needing to use touch to create the file before.
And as you can pass options to Linux commands, echo has -e that instructs it to interpret special chars like \n in your string. So you can send multiple lines to a file with echo -e "line1\nline2\n..." > somewhere.txt.

Shell script that inserts text to last line of a file based on user input

#!/bin/sh
...
read var
#user enters: it doesn't work
...
echo 'Some text and $var' > myfile.txt
Expected output:
cat myfile.txt
#Some text and it doesn't work
Actual output:
cat myfile.txt
#Some text and $var
So how to echo the content into the file with the value of the $var variable?
use double quote instead of simple quote to make variable remplacement available
you can also remove your quotes to make it work but it's not recommanded
http://wiki.bash-hackers.org/syntax/quoting
if you want to insert $var to last line(=append the content $var to file), please use >>
should be:
echo "Some text and $var" >> myfile.txt
the > is to override the content, whereas >> is to append the content to a file

Bash: redirect `cat` to file without newline

I'm sure this question has been answered somewhere, but while searching for it, I can't find my exact scenario. I have two files that I am concatenating into a single file, but I am also adding user input between the two files. The problem is that a newline is being inserted after the first file, the rest of it works as desired.
touch newFile
cat file1 > newFile
echo -n $userInput >> newFile
cat file2 >> newFile
How do I prevent or remove the newline when file1 is added to newFile? If I cat file1 there seems to be a newline added by cat but everything I see about cat says it doesn't do that. If I vim file1 there's not a blank line at the end of the file that would indicate the newline is a part of the file, so either cat is actually adding a newline, or the redirect > is doing it, or echo adds a newline at the beginning of its output, none of which would be desirable in this situation. One solution I saw was to use
cat file1 | tr -d '\n'
but that discards all the newlines in the file, also not desirable. So, to repeat my question:
How do I cat file1 into the new file and add user input without adding the newline between them?
(cat is not a requirement, but I am not familiar with printf, so if that's the solution then please elaborate on its use).
With these inputs:
userInput="Test Test Test"
echo "Line 1
Line 2
Line 3" >file1
echo "Line 4
Line 5
Line 6" >file2
I would do:
printf "%s%s%s" "$(cat file1)" "$userInput" "$(cat file2)" >newfile
The creation of >newfile is equivalent to touch and adding content in your first step. A bit easier to see intent with this.
I get:
$ cat newfile
Line 1
Line 2
Line 3Test Test TestLine 4
Line 5
Line 6
Like all other Unix tools, Vim considers \n a line terminator and not a line separator.
This means that a linefeed after the last piece of text will be considered part of the last line, and will not show an additional blank line.
If there is no trailing linefeed, Vim will instead show [noeol] in the status bar when the file is loaded:
foo
~
~
~
~
~
"file" [noeol] 1L, 3C 1,1 All
^---- Here
So no, the linefeed is definitely part of your file and not being added by bash in any way.
If you want to strip all trailing linefeeds, you can do this as a side effect of command expansion:
printf '%s' "$(<file1)" >> newfile
touch newFile
echo -n "$(cat file1)" > newFile
echo -n $userInput >> newFile
cat file2 >> newFile
That did the trick.

Linux bash Shell scripting : Creating command using string Append how to do that?

Hi I want to achieve #cat sample.txt > abc.txt
But "> abc.txt" am getting as argument.
How to cascade these two strings and execute the combined one.
I tried the below steps
/home/root# export str=" > abc.txt;"
/home/root# echo $str
> abc.txt;
/home/root# echo "cat sample.txt $str"
cat sample.txt > abc.txt;
/home/root# `echo "cat sample.txt $str"`
Hello world
cat: can't open '>': No such file or directory
cat: can't open 'abc.txt;': No such file or directory
like this?
#!/bin/bash
args="> foo"
command="date"
eval "$command $args"
cat foo
use of eval is not really a recommended method but sometimes it comes in handy for doing things in a quick and dirty way. I will probably get downrated for suggesting this.
Be aware of its side effects.
If you need to do this from command line write :
cat sample.txt | tee $str

Formatting text in shell scripting

from the command line to redirect an ouptput to another file I am aware that I can do something like this
$ echo randomText > file.md
I am also aware that, if I want to append the output to the end of the file, I can do something like this
$ echo randomText >> file.md
Now if I cat the content of file.md I will see something like
randomText
randomText
Is there a way to format the output that is being sent to the file. Rather than appending to the end I am hoping to achieve something like this
randomText ----------------------------------- randomText
To do this, I used printf to format the ouput that was being sent to the file.
printf "%10s", "------------------------------------------" > file.md
To append to the same line, yuou could use printf to tab it.
While going through loop you can try this
echo -ne "randomText" >> logfile
# some other actions
echo -ne "--------------------------------------" >> logfile
echo -ne "randomText" >> logfile
In logfile you can now find
randomText--------------------------------------randomText

Resources