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
Related
I just have written a simple logger which append a message with time to a file. Now I also want to add error out to that log file for better understanding what went wrong. Here is my current code:
#!/bin/bash
logprint() {
echo "$(date +%T): $*" >> ./logfile
}
logdate() {
DATE=`date "+%d.%m.%Y"`
echo "-------------------- ${DATE} --------------------" >> ./logfile
}
The log print function takes arguments and simply write the date plus the message to the log file. The log date function simply writes the date at the beginning.
Now I would like to give the error output to the log print function. Whats the best way to do that?
You can use process substitution technique of form > >(cmd) for this. This allows you to re-direct the output from the standard error stream to the function. You can do something like
2> >(logprint)
But you can't read from the output of this process-substitution as if you were reading from the positional arguments, you need to read as if you were reading over standard input. You can tweak your function to something like below. Added a script for demonstration purposes
#!/usr/bin/env bash
logprint() {
args=""
if (( "$#" > 0 )); then
args="$*"
else
while IFS= read -r line; do
args+="$line"
done
fi
echo "$(date +%T): $args" >> ./logfile
}
logprint "foobar"
mv foobar nosuchfile 2> >(logprint)
If you need the timestamp to be in the same line, the simplest way I can think of is partially filling the line with timestamp (without ending with a newline) and then redirect the error output.
echo -n "$(date) " >> error.txt
ls no_such_file_here 2>> error.txt # an error message is generated here
echo "" >> error.txt # add a newline (useful in case an error message is not produced above)
Note that the last echo is to add a new line character to the current line as it guarantees anything appended later will not be added to the same line.
(However, that can result in an empty line if an error is generated in the line above.)
Update:
I was assuming that you are referring to the STDERR stream. However if that is not the case, the same idea can be used.
#!/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
So I am creating a variable and I want to echo it with an addition to the end like so:
I have a file: Filename-08-10-2017.txt
I create a variable:
myvariable=Filename*.txt
When I echo that variable:
echo $myvariable
it outputs Filename-08-10-2017.txt
But I want to change the name to .zip
So I am trying to go:
echo $myvariable.zip and have it output Filename-08-10-2017.txt.zip
however it outputs:
Filename*.txt.zip
How do I go about having it output the way I want?
Thanks.
EDIT:
I kind of figured it out.
I saved a new variable as $($myvariable) which saved the output.
The file name comes from glob expansion. If you want to iterate all files by using glob expansion, you can :
myvariable=Filename*.txt
for f in $myvariable; do echo $f; done
If you want to "disable" the glob expansion, e.g. get literial Filename*.txt by echo $myvariable, you can either set -f or just wrap the variable by double quote: echo "$myvariable".
To maniuplate the text you can do something like:
for f in $myvariable; do echo $f"whatever_like_zip"; done
if you want to do some text substitution, you can
for ... echo ${f/%txt/zip} ; done
It will change all txt file name to zip.
Also if you want to rename the file, change the above echo... into
mv "$f" "yourNewNameHere"
Anyway by reading your question I'm not quite clear, what do you really want.
Yuo can use sed
try this
file="test.txt"
newext=$(echo "$file" | sed -e "s|txt|zip|g")
echo $newext
I have an executable that takes a file and outputs a line.
I am running a loop over a directory:
for file in $DIRECTORY/*.png
do
./eval $file >> out.txt
done
The output of executable does not contain the name of the file.
I want to append the file name with each output.
EDIT1
Perhaps, I could not explain it correctly
I want the name of the file and the output of the program as well, which is processing the same file, Now I am doing following
for file in $DIRECTORY/*.png
do
echo -n $file >> out.txt
or
printf "%s" "$file" >> out.txt
./eval $file >> out.txt
done
For both new line is inserted
If I understood your question, what you want is:
get the name of the file,
...and the output or the program processing the file (in your case, eval),
...on the same line. And this last part is your problem.
Then I'd suggest composing a single line of text (using echo), comprising:
the name of the file, this is the $file part,
...followed by a separator, you may not need that but it may help further processing of the result. I used ":". You can skip this part if this is not interesting for you,
...followed by the output of the program processing the file: this is the $(...) construct
echo $file ":" $(./eval $file) >> out.txt
...and finally appending this line of text to a file, you got that part right.
please use like this
echo -n `echo ${file}|tr -d '\n'` >> out.txt
OR
newname=`echo ${file}|tr -d '\n'`
echo -n $newname >> out.txt
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