LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   printing a character to file (https://www.linuxquestions.org/questions/linux-newbie-8/printing-a-character-to-file-747578/)

Completely Clueless 08-14-2009 12:14 PM

printing a character to file
 
Hi all,

Any script kiddies out there?

what's the best way to send a single text character (append it) to a text file. I've tried:

$char >> textfile.txt

but Bash doesn't understand it. I then tried:

write $char >> textfile.txt

But still no go. Similarly:

putch$char >> textfile.txt

and no joy either.

How should it be done properly?

thanks, cc.

MensaWater 08-14-2009 12:16 PM

echo $char >>textfile.txt

All this assumes you've previously defined the variable $char (e.g. char=b).

catkin 08-14-2009 12:24 PM

man write
write - send a message to another user

$ man putch
No manual entry for putch
$ putch
bash: putch: command not found

???

Completely Clueless 08-14-2009 12:53 PM

Quote:

Originally Posted by jlightner (Post 3643418)
echo $char >> textfile.txt

All this assumes you've previously defined the variable $char (e.g. char=b).

Thanks. It doesn't seem to work if the character in question is a space, however. Spaces get stripped from the output file. Any way of fixing that?

Completely Clueless 08-14-2009 12:54 PM

Quote:

Originally Posted by catkin (Post 3643431)
man write
write - send a message to another user

$ man putch
No manual entry for putch
$ putch
bash: putch: command not found

???

Good point. I forgot about "printf" :-)

catkin 08-14-2009 12:58 PM

Code:

echo "$char" >> textfile.txt
Generally squeaking it's good practice to always double quote variables to avoid such gotchas.

And I forgot printf, too :(

Completely Clueless 08-14-2009 02:26 PM

Quote:

Originally Posted by catkin (Post 3643482)
Code:

echo "$char" >> textfile.txt
Generally squeaking it's good practice to always double quote variables to avoid such gotchas.

And I forgot printf, too :(

I did try double and single quotes before making that post. I know they can have a big impact on formatting. However, still no joy. Everything comes out space-expanded. I mean letters between individual words become single-spaced; words are separated by double spaces! You can't win!

I reckon C is far more suited to this particular task, so I'm going to switch at this point. Thanks for all the ideas anyway, guys.

Completely Clueless 08-14-2009 02:40 PM

One more thing I thought I should add. Here is the source code:

Code:

#!/bin/bash
#process a text file character-by-character and send the output to
#a separate file. Input filename is textfile; output filename is
#outputfile. No character translations required at this stage.
# the variable '$char' is the individual file character under test

touch outputfile ; chmod 666 outputfile


while read -n1 char
do

  echo -n "$char" >> outputfile


done < textfile.txt

It should just produce a copy of a text file in the same directory. In practice however, the 'copy file' is MUCH larger than the original, appears to have been processed several times in different ways, and contains a lot of gobbldegook that looks astonishly like the output of ps -e processes listing!!

I'll post the input and output files if anyone's interested in finding out why this happens.

Completely Clueless 08-14-2009 02:58 PM

Ah! I wonder if the problem could be due to me using 'touch' to create an empty file to accept the output? Maybe on subsequent re-runs of the program, it won't overwrite a file of the same name? Not much info in the man page. :-(

nuwen52 08-14-2009 03:34 PM

Quote:

Originally Posted by Completely Clueless (Post 3643586)
Ah! I wonder if the problem could be due to me using 'touch' to create an empty file to accept the output? Maybe on subsequent re-runs of the program, it won't overwrite a file of the same name? Not much info in the man page. :-(

I don't think that's a problem. The only thing touch will do on a pre-existing file is to update it's access/modification times. Touch will not delete everything in the file if it exists. A second/third/etc. run would just create an ever growing output file.

I ran this program, and it created a copy of a file, except for all white-space characters were removed.

Completely Clueless 08-14-2009 04:18 PM

Quote:

Originally Posted by nuwen52 (Post 3643626)
I don't think that's a problem. The only thing touch will do on a pre-existing file is to update it's access/modification times. Touch will not delete everything in the file if it exists. A second/third/etc. run would just create an ever growing output file.

I ran this program, and it created a copy of a file, except for all white-space characters were removed.

Yeah, but that's what i'm getting at. Try changing the program in some small way so it will produce a different output and see if the output file is the same. in my experience it will be. Touch has not overwritten it, nor given any warning it hasn't.

catkin 08-14-2009 04:27 PM

What's the problem?
Code:

#!/bin/bash

echo -n 'abcdef' > input.txt
cat /dev/null > output.txt
while read -n1
do
        echo -n "$REPLY" >> output.txt
done < input.txt

# The following command produced no output if the files are identical
diff input.txt output.txt

The only problem is if there is a newline at the end of input.txt; it is not copied.

Completely Clueless 08-14-2009 04:36 PM

Quote:

Originally Posted by catkin (Post 3643672)
What's the problem?
Code:

#!/bin/bash

echo -n 'abcdef' > input.txt
cat /dev/null > output.txt
while read -n1
do
        echo -n "$REPLY" >> output.txt
done < input.txt

# The following command produced no output if the files are identical
diff input.txt output.txt

The only problem is if there is a newline at the end of input.txt; it is not copied.

Sorry, but where is $REPLY defined/assigned/declared?

catkin 08-14-2009 04:52 PM

$REPLY is the default variable set by the read command.

I can't find it in the Gnu bash Reference or in the POSIX Shell Reference but it's in the Advanced Bash-Scripting Guide and the bash man page: "If no names are supplied, the line read is assigned to the variable REPLY".

Completely Clueless 08-15-2009 05:02 AM

Okay, thanks for the clarification, Charles. However, as I stated earlier in this thread......

"I reckon C is far more suited to this particular task, so I'm going to switch at this point. Thanks for all the ideas anyway, guys."


All times are GMT -5. The time now is 04:07 AM.