LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-30-2012, 10:43 AM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Rep: Reputation: 76
Writing a file character by character with a bash builtin command (script).


Hi:

The Bash read builtin command can read a file character by character (read -n 1). Is there an analogous command for writing?
 
Old 06-30-2012, 11:43 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
My first reaction is WHY?

Assuming there's a good reason to do this, let's clarify: for example, do you want to take lines of data and write that data one character at a time?--If so, do you want to keep the line structure (as opposed--eg--to writing every character to a new line)?

Perhaps you could post an example data snippet and then describe exactly what the system should do.


Meanwhile:
Code:
dd if=somefile of=otherfile bs=1
 
Old 06-30-2012, 12:59 PM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Take this:

To be or not to be,
that is the question.

Whether 'tis nobler in the mind
to suffer the slings and arrows or outrageous fortune

Assume further that I want to post those lines in a forum or mail them to a friend. Also, I do not like using the GUI to write; so, I compose my writings in a virtual terminal.

Now, if I simply cut and paste that text into the page window (that of the forum), most probably it will look bad formated. By this I mean I won't be seeing it as above. The reason is as I type in the terminal, I enter newlines whereas as one types in the editor window of a forum no newlines are inserted unless one presses <Enter>.

So, what do I do? I run
Code:
cat text_just_written.txt|tr '\n' ' '>text_just_written.stripped
stripping the file of all its newlines, not before intercalating a special mark ('[', left square bracket) where the are two consecutive newlines, as you will be able to see in the example above, between "that is the question." and "Whether 'tis nobler in the mind". Otherwise tr would make me lose that information.

As a final step, I edit *.stripped and replace '[' by two newlines, cut and paste and ... ready to send. This procedure involves a lot of work made by hand. And as at the same time I want to improve my script writing skills, I thought a script would be the best thing to do.

I look forward to have answered your question and and to listen to your criticisms. Also, I'm sending you a link to another post of mine, which has been replied by you, and propose to continue the discussion in that tread. In that post, you'll get a better idea about what I intend to do. The link:

http://www.linuxquestions.org/questi...5/#post4715764


Regards.

Last edited by stf92; 06-30-2012 at 01:05 PM.
 
Old 06-30-2012, 08:18 PM   #4
dru8274
Member
 
Registered: Oct 2011
Location: New Zealand
Distribution: Debian
Posts: 105

Rep: Reputation: 37
Code:
echo -e "Well,\nI hope that\nworks out for you." | pv -qL 10
 
Old 06-30-2012, 08:41 PM   #5
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Quote:
Originally Posted by dru8274 View Post
Code:
echo -e "Well,\nI hope that\nworks out for you." | pv -qL 10
It's been very kind of you dru8274, only that I lack the pv command (nor do I see it as a bash builtin). But it's my fault for not having specified either my distro version or the bash version. Regards.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find and Replace character/special character from the file MyRelam Red Hat 8 05-21-2012 12:52 AM
Bash scripting: parsing a text file character-by-character Completely Clueless Programming 13 08-12-2009 09:07 AM
bash command to count particular character in a string kkpal Linux - Newbie 5 05-27-2008 06:39 AM
bash script to rename files removing one character Byenary Linux - Newbie 2 04-08-2008 10:12 AM
escaping ':' character in bash script paulyche Linux - General 2 11-07-2006 07:18 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration