tawheed
tawheed

Reputation: 5821

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

Upvotes: 0

Views: 2720

Answers (2)

tawheed
tawheed

Reputation: 5821

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.

Upvotes: 1

deagh
deagh

Reputation: 2722

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

Upvotes: 0

Related Questions