Adding a new line to a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding a new line to a script
# 1  
Old 10-24-2017
Adding a new line to a script

Good Morning,

I have a log file on a solaris 9 machine that gets appended this way:

Code:
printf "\nBlahBlahBlah - $TIME"

but it doesn't create a new line. I have to go into the file and split the bottom line up manually. Any ideas why
Code:
\n

isn't doing what I want?
# 2  
Old 10-24-2017
You mean there's no \n before BlahBlahBlah or after $TIME at the end of the file? It's usual to put the \n after what you want to print, not before it (e.g. printf "BlahBlahBlah - $TIME\n"). As it is, anything added to the end of the file (not added by your printf with its leading \n) will go directly after $TIME, on the same line.
This User Gave Thanks to Scott For This Post:
# 3  
Old 10-24-2017
Do you actually run
Code:
printf ... >> logfile

?
This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 10-26-2017
Quote:
Originally Posted by Scott
You mean there's no \n before BlahBlahBlah or after $TIME at the end of the file? It's usual to put the \n after what you want to print, not before it (e.g. printf "BlahBlahBlah - $TIME\n"). As it is, anything added to the end of the file (not added by your printf with its leading \n) will go directly after $TIME, on the same line.
OK I made that change, we'll see what happens.


Thanks everyone.. Found the problem. This issue was Notepad's treatment of
Code:
\n

. Apparently, you have to add a carriage return for a lot of applications, so -
Code:
\r\n blahblahblah

will put the text on a new line.
# 5  
Old 10-26-2017
No. This is true ONLY for MS environments. Any *nix style system uses \n exclusively; in fact, the presence of \r in data files or scripts is the cause for many errors.
Another reason to insist on people showing their system / environment...
This User Gave Thanks to RudiC For This Post:
# 6  
Old 10-30-2017
Sweet..it worked well over the weekend on all the scripts except one. That script used
Code:
echo

to write to the file instead of
Code:
printf

so I changed that one for next time. Apparently
Code:
echo

has fewer formatting options.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding text to the first line of a shell script

the first line of every unix script written in an interpreted language always has a "#!<path-to-the-language>" is there a way to include other text in that first line without it affecting the ability of the script to run??? for instance, if i change the following line: #!/bin/sh echo blah... (1 Reply)
Discussion started by: SkySmart
1 Replies

2. UNIX for Beginners Questions & Answers

Adding a new line after a specific line with sed

Hi All, My requirement is to add a specific line in a file after a certain line that contains 'setenv' the existing code is like setenv SEQFILES "/ConvWrk/inteng03/alltars/bnymais1" LIBDEF scope='JOB' type='PGM' dataset='SUNAR.PJ90000P.JOBLIB'... (5 Replies)
Discussion started by: gotamp
5 Replies

3. Shell Programming and Scripting

Adding line in a file using info from previous line

I have a shell script that looks something like the following: mysql -uroot db1 < db1.sql mysql -uroot db2 < db2.sql mysql -uroot db3 < db3.sql mysql -uroot db4 < db4.sql .... different db names in more than 160 lines. I want to run this script with nohup and have a status later. So,... (6 Replies)
Discussion started by: MKH
6 Replies

4. Shell Programming and Scripting

Adding comma to end of each line if more than 1 line

I have a file with dates as '2013-01-01' '2013-01-02' I want the output to be '2013-01-01','2013-01-02' if there is only 1 entry then there should not be any comma. (6 Replies)
Discussion started by: ATWC
6 Replies

5. Shell Programming and Scripting

Adding tab/new line at the end of each line of a file

Hello Everyone, I need a help from experts of this community regarding one of the issue that I am facing with shell scripting. My requirement is to append char's at the end of each line of a file. The char that will be appended is variable and will be passed through command line. The... (20 Replies)
Discussion started by: Sourav Das
20 Replies

6. Shell Programming and Scripting

Script adding ^M to end of line

I trying to make a simple script to get info from remote servers my problem is the output of this line- SERVER_NAME=`ssh -t $USER@$REMOTESERVER 'hostname'`the output is linux1^M I would like to remove the ^M where is my error? Many Thanks -Steve (1 Reply)
Discussion started by: shoodlum
1 Replies

7. Shell Programming and Scripting

Script for adding a word in front of all line in a file

Hi I've one file full of paths of certain files and I want to add some extra file words in front of all the paths. for eg: i have a file name test.txt which show some details only.. 024_hd/044/0344eng.txt 035_bv/222/editor.jpg here I want to add /usr/people/indiana/ infront of all the... (4 Replies)
Discussion started by: ratheeshp
4 Replies

8. Shell Programming and Scripting

Adding a blank line after every 5th line

Hello... I have a file which contain certain number of records. I want to generate another file from this file which will contain 1st line as a blank line & after every 5 lines one blank line will be inserted. How to achieve this through shell scripting? Thanks... (5 Replies)
Discussion started by: 46019
5 Replies

9. Shell Programming and Scripting

adding characters end of line where line begins with..

Hi all, using VI, can anyone tell me how to add some characters onto the end of a line where the line begins with certain charactars eg a,b,c,......., r,s,t,........, a,b,c,......., all lines in the above example starting with a,b,c, I want to add an x at the end of the line so the... (6 Replies)
Discussion started by: satnamx
6 Replies

10. Shell Programming and Scripting

Fill the empty line by adding line before blank line

FIle A "A" 2 aa 34 3 ac 5 cd "B" 3 hu 67 4 fg 5 gy output shud be A"" 2 aa 34 "A" 3 ac 34 "A" 5 cd 34 "B" 3 hu 67 "B" 4 fg 67 "B" 5 gy 67 (6 Replies)
Discussion started by: cdfd123
6 Replies
Login or Register to Ask a Question