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."

catkin 08-15-2009 06:41 AM

Quote:

Originally Posted by Completely Clueless (Post 3644176)
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."

Your almost certainly right (not knowing what this particular task is, I can't be sure but C is way faster than shell) but I didn't want you to change from bash to C because you thought bash could not do it.

Completely Clueless 08-15-2009 07:31 AM

Quote:

Originally Posted by catkin (Post 3644258)
Your almost certainly right (not knowing what this particular task is, I can't be sure but C is way faster than shell) but I didn't want you to change from bash to C because you thought bash could not do it.

That's fine, Charles. I'm sure Bash could do it, but it's more of a C job IMHO. I'm increasingly aware of the strengths and weaknesses of Bash, will continue to learn it and respect it for what it does well. At the same time, I'm going to dust off my rusty old C coding experience and polish up on that, too.

Pushing 90 degrees here again today I'm sorry to say. Now where did I put those salt tablets?

MensaWater 08-15-2009 09:02 AM

If you like C and want to do scripting very similar to it you might want to investigate Perl.

Completely Clueless 08-15-2009 10:27 AM

Quote:

Originally Posted by jlightner (Post 3644364)
If you like C and want to do scripting very similar to it you might want to investigate Perl.

Sorry, you'll have to excuse my ignorance, but what advantage would that confer?

catkin 08-15-2009 10:56 AM

Quote:

Originally Posted by Completely Clueless (Post 3644417)
Sorry, you'll have to excuse my ignorance, but what advantage would that confer?

Familiarising oneself with 3 languages at the same time has great confusion potential++

Completely Clueless 08-15-2009 11:08 AM

Quote:

Originally Posted by catkin (Post 3644442)
Familiarising oneself with 3 languages at the same time has great confusion potential++

Too true, Charles! That's why I am determined to strenuously avoid the psuedo-C constructs that Bash can parse. The syntax isn't *quite* the same, which certainly could cause problems for me in both.

catkin 08-15-2009 01:35 PM

Quote:

Originally Posted by Completely Clueless (Post 3644454)
Too true, Charles! That's why I am determined to strenuously avoid the pseudo-C constructs that Bash can parse. The syntax isn't *quite* the same, which certainly could cause problems for me in both.

Which would make PERL an ideal choice to maximise confusion; it looks like a mixture of shellscript and C with its own pathologically eclectic rubbish thrown in for good measure.

w1k0 08-15-2009 04:52 PM

Quote:

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

The problem is your script removes new lines characters. To avoid it you have to change delimiter:

Code:

#!/bin/sh

> outfile.txt

while read -n1 -d "^["
do
    echo -n "$REPLY" >> outfile.txt
done < infile.txt


w1k0 08-16-2009 11:10 AM

"^[" in the above script doesn't mean two characters: ^ and [ but one character: escape. To get it you could use vim. It's enough to go to editing mode, to press Ctrl-V for literal mode, and to press Esc for escape character.

If you're not acquainted with vim try that sequence:

Open the script with the command: vim your_script_name.
Go to the desired position using arrows.
Press i (lowercase i) for insert mode.
Press Ctrl-V (Ctrl and v) for literal mode.
Press Esc for escape character.
Press Esc to exit editing mode.
Press ZZ (uppercase Z twice) to save changes and exit vim.


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