Replace unix 'new line' with windows 'new line' | 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.

Replace unix 'new line' with windows 'new line'

Shell Programming and Scripting

convert unix windows

Closed Thread    
Thread Tools Search this Thread Display Modes
Old 10-20-2010
wha72 wha72 is offline
Registered User
Join Date: Oct 2010
Last Activity: 21 June 2011, 6:34 PM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Replace unix 'new line' with windows 'new line'

I'm trying to write a script to convert a unix text file to windows/dos format. Essentially all I want to do is replace the Unix return with a windows one.

on the command line I can do this successfully by:

sed s/$/^M/ ~/unix.txt ~/dos.txt

when I put this line in my shell script it literally enters a ^M in the text file.
Sponsored Links
Old 10-20-2010
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Join Date: Dec 2003
Last Activity: 27 February 2015, 4:23 PM EST
Location: /dev/ph
Posts: 4,884
Thanks: 68
Thanked 445 Times in 413 Posts
Unfortunately you cannot do it that way.

From SED oneliners
# IN UNIX ENVIRONMENT: convert Unix newlines (LF) to DOS format.
sed "s/$/`echo -e \\\r`/" # command line under ksh
sed 's/$'"/`echo \\\r`/" # command line under bash
sed "s/$/`echo \\\r`/" # command line under zsh
sed 's/$/\r/' # gsed 3.02.80 or higher
Sponsored Links
Old 10-21-2010
thegeek thegeek is offline
Read Only
Join Date: Apr 2009
Last Activity: 30 July 2012, 5:38 AM EDT
Location: /usr/bin/vim
Posts: 946
Thanks: 13
Thanked 38 Times in 36 Posts
normally i use, dos2unix, unix2dos commands for those conversions.
Old 10-21-2010
durden_tyler's Avatar
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
Join Date: Apr 2009
Last Activity: 27 February 2015, 11:53 AM EST
Posts: 1,859
Thanks: 7
Thanked 271 Times in 246 Posts
Alternatively, you could use Perl -

perl -i.bak -pe 's/\n/\r\n/g' unix.txt

Sponsored Links
Old 10-21-2010
methyl methyl is offline Forum Advisor  
Join Date: Mar 2008
Last Activity: 12 February 2015, 7:41 PM EST
Posts: 6,397
Thanks: 288
Thanked 674 Times in 643 Posts
sed s/$/^M/ ~/unix.txt ~/dos.txt
You can generate the carriage return character in Shell:

CR=`echo "\0015"`          # Carriage Return
sed -e "s/$/${CR}/g" ~unix.txt > ~dos.txt

Sponsored Links
Old 10-21-2010
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Join Date: Feb 2007
Last Activity: 26 February 2015, 7:59 AM EST
Location: The Netherlands
Posts: 7,727
Thanks: 128
Thanked 543 Times in 511 Posts

awk '{printf("%s\r\n",$0)}' unix_file > dosfile

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
Search Parameter in first line and replace next line content rdtrivedi UNIX for Advanced & Expert Users 4 03-24-2010 01:03 PM
how to replace a line in file with blank line tarunn.dubeyy Shell Programming and Scripting 5 09-10-2009 10:13 AM
Need to replace the first word of a line if it occurs again in the next line(shell) geeko Shell Programming and Scripting 4 06-18-2009 03:36 PM
how to replace a text of line with a comment line manoj.b UNIX for Dummies Questions & Answers 6 05-21-2009 12:11 PM
sed - Replace Line which contains the Pattern match with a new line kousikan Shell Programming and Scripting 2 03-24-2007 08:24 AM

All times are GMT -4. The time now is 08:31 PM.