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

  Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

How to add newline character at end of file?

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-27-2013
jnrohit2k jnrohit2k is offline
Registered User
 
Join Date: May 2013
Last Activity: 29 May 2015, 12:11 PM EDT
Posts: 21
Thanks: 3
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 Unix and Linux 06-27-2013
shamrock shamrock is offline Forum Advisor  
Registered User
 
Join Date: Oct 2007
Last Activity: 8 June 2016, 4:39 PM EDT
Location: USA
Posts: 1,577
Thanks: 30
Thanked 152 Times in 143 Posts
Before you go on do read this post on why not csh.
Sponsored Links
    #3  
Old Unix and Linux 06-27-2013
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 24 July 2016, 3:53 PM EDT
Location: Simplicity
Posts: 3,153
Thanks: 243
Thanked 1,006 Times in 910 Posts
Does the perl script really work?
IMHO should be

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

    #4  
Old Unix and Linux 06-27-2013
jnrohit2k jnrohit2k is offline
Registered User
 
Join Date: May 2013
Last Activity: 29 May 2015, 12:11 PM EDT
Posts: 21
Thanks: 3
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 Unix and Linux 06-28-2013
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 24 July 2016, 3:53 PM EDT
Location: Simplicity
Posts: 3,153
Thanks: 243
Thanked 1,006 Times in 910 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 Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 08:15 PM.