Echo to file using SH without adding newline character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Echo to file using SH without adding newline character
# 1  
Old 06-14-2012
Echo to file using SH without adding newline character

Hello!

I am able to do this in bash, using:
Code:
echo -ne HELLO > file.txt

and then, 'HELLO' is written into file.txt without the newline character to be added in the end of the file.

How is this possible to be done using sh instead of bash?
If I try something similar is SH, then inside file.txt, I will find:
Code:
-ne HELLO

which is quite funny :P

Any help appreciated!Smilie
# 2  
Old 06-14-2012
The 'printf' builtin is standard, and the same everywhere. It never prints newlines unless you give it \n's.

Code:
printf "hello" >> filename

# 3  
Old 06-14-2012
Yes, this is what I want Smilie
Thanks a lot for this!!!!
# 4  
Old 06-14-2012
Just for completeness.
The syntax for echo has two main variants. The BSD style uses echo -n "HELLO" and the rest use echo "HELLO\c" .

The excellent Mascheck site has a comprehensive comparison of the variants of [/icode]echo[/icode] and printf .
echo and printf behaviour
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove newline character from column spread over multiple lines in a file

Hi, I came across one issue recently where output from one of the columns of the table from where i am creating input file has newline characters hence, record in the file is spread over multiple lines. Fields in the file are separated by pipe (|) delimiter. As header will never have newline... (4 Replies)
Discussion started by: Prathmesh
4 Replies

2. Shell Programming and Scripting

How to remove newline character if it is the only character in the entire file.?

I have a file which comes every day and the file data look's as below. Vi abc.txt a|b|c|d\n a|g|h|j\n Some times we receive the file with only a new line character in the file like vi abc.txt \n (8 Replies)
Discussion started by: rak Kundra
8 Replies

3. Shell Programming and Scripting

Warning while sorting : A newline character was added to the end of file

Hi, I am trying to sort a csv file which has say 10 lines each line having a row size that is upto 30183 no. of COLUMNS (Row length = 30183). There is a LINE FEED (LF) at the end of each line. When I try to sort this file say, based on the second FIELD using the below command, sort -t ',' +1... (5 Replies)
Discussion started by: DHeisenberg
5 Replies

4. Shell Programming and Scripting

Script for removing newline character from file

Hi below is my file. cat input.dat 101,abhilash,1000 102,prave en,2000 103,partha,4 000 10 4,naresh,5000 (its just a example file) and my output should be: 101,abhilash,1000 102,praveen,2000 103,partha,4000 104,naresh,5000 below is my code cat input.dat |tr -d '\n' >... (6 Replies)
Discussion started by: abhilash_nakka
6 Replies

5. Shell Programming and Scripting

Appending newline character End of File

Hi Gurus, Need help. I'm a beginner in Unix. I have a requirement, need to add or append newline (\n) character in file. Sample Data: 1|Main|Test|~# 2|Main|Hello|~# 3|Main|Unix|~# 4|Main|File|~#Output: 1|Main|Test|~# 2|Main|Hello|~# 3|Main|Unix|~# 4|Main|File|~#\n -- append only... (13 Replies)
Discussion started by: Gouri Solleti
13 Replies

6. Shell Programming and Scripting

How to add newline character at end of file?

Hi All, I have following piece of code in UNIX C Shell script and I want to add one more command which can add newline at the end of file only if there is no newline character exists. foreach file (`ls $dd_PLAYCARD_EDI_IN`) if ( -f $dd_PLAYCARD_EDI_IN/${file} ) then cat -n... (4 Replies)
Discussion started by: jnrohit2k
4 Replies

7. Shell Programming and Scripting

How to check newline character in file?

Hi All, I have file with only one record,always be only one record. as like below. if that line contains newline end of the line..no need to add, if not just add the new line character. END OF FILE. ROW COUNT: 7 Please help me.. Thanks, (9 Replies)
Discussion started by: bmk
9 Replies

8. Shell Programming and Scripting

How to avoid Newline character in generated text file?

Hi All, Just need small help in resolving the special new line character in generated output file. In one of my shell script I am using following lines to get the spool file (i.e. sfile.txt) and AAA_XXXX_p111_n222.txt AAA_YYYY_p111_n222.txt Here assuming v_pnum="p111" v_nid="n222" ... (1 Reply)
Discussion started by: shekharjchandra
1 Replies

9. Shell Programming and Scripting

To remove the newline character while appending into a file

Hi All, We append the output of a file's size in a file. But a newline character is appended after the variable. Pls help how to clear this. filesize=`ls -l test.txt | awk `{print $5}'` echo File size of test.txt is $filesize bytes >> logfile.txt The output we got is, File size of... (4 Replies)
Discussion started by: amio
4 Replies

10. UNIX for Dummies Questions & Answers

echo without newline character

hi, I have a for loop where in I write some file name to another file. I want to write all the filenames to another without any newlines. how can i avoid getting new lines with echo? Thanks, Srilaxmi (2 Replies)
Discussion started by: srilaxmi
2 Replies
Login or Register to Ask a Question