How to add newline character at end of file? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

How to add newline character at end of file?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-27-2013
jnrohit2k jnrohit2k is offline
Registered User
 
Join Date: May 2013
Last Activity: 16 March 2014, 7:24 PM EDT
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
How to add newline character at end of file?


Code:
 
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 "$dd_PLAYCARD_EDI_IN/${file}"    | \
      perl -p -e "s/\x0d\x0a/\x0a/"  | \
      sed -e 's/00000000$/22991231/' | \
      sort -k1.18,1.29               >   $DATADIR/playcard/$file

Above "for loop" retrive the file from a directory then "cat" on each file to add line number, "perl" to remove ^M (control M) characters from the end of each line, "sed" will replace last 8 zeros with 22991231 and finally sort will sort the file. After "perl" command I want to add command which can add newline character at the end of file if it do not exists.
 
Please suggest.
 
Thanks!

Sponsored Links
    #2  
Old 06-27-2013
shamrock shamrock is offline Forum Advisor  
Registered User
 
Join Date: Oct 2007
Last Activity: 25 October 2014, 1:34 PM EDT
Location: USA
Posts: 1,492
Thanks: 20
Thanked 146 Times in 138 Posts
Before you go on do read this post on why not csh.
Sponsored Links
    #3  
Old 06-27-2013
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 25 October 2014, 4:43 PM EDT
Location: Simplicity
Posts: 1,978
Thanks: 127
Thanked 578 Times in 524 Posts
Does the perl script really work?
IMHO should be

Code:
perl -p -e 's/\x0d$//'

    #4  
Old 06-27-2013
jnrohit2k jnrohit2k is offline
Registered User
 
Join Date: May 2013
Last Activity: 16 March 2014, 7:24 PM EDT
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts

Code:
 
I tried below command and it worked but it is adding one more line if there is already a blank line at the end of file.
 
perl -p -e "s/$/\x0a/"
 
I also cannot use any other Shell. Let me know if you have any other way to do it.
 
Thanks!

Sponsored Links
    #5  
Old 06-28-2013
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 25 October 2014, 4:43 PM EDT
Location: Simplicity
Posts: 1,978
Thanks: 127
Thanked 578 Times in 524 Posts
The -l option might do the trick:

Code:
perl -lpe 's/\x0d$//'

Please use 'ticks' not "quotes" for embedding perl code!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to add newline before and after a special character? ShiGua UNIX for Dummies Questions & Answers 2 12-11-2012 08:18 PM
add character to every end-of line in file naamas03 Shell Programming and Scripting 8 11-03-2009 02:04 AM
add character to the end of each line in file naamas03 Shell Programming and Scripting 7 07-27-2009 02:08 AM
How to remove a newline character at the end of filename amio Shell Programming and Scripting 2 02-10-2009 04:18 AM
How to add new line character at the end of a file DebianJ UNIX for Dummies Questions & Answers 5 05-07-2008 03:22 PM



All times are GMT -4. The time now is 06:50 PM.