Unix/Linux Go Back    

Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

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

Shell Programming and Scripting

convert unix windows

Thread Tools Search this Thread Display Modes
Old Unix and Linux 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 Unix and Linux 10-20-2010
fpmurphy's Unix or Linux Image
fpmurphy fpmurphy is offline Forum Advisor  
Join Date: Dec 2003
Last Activity: 12 June 2016, 11:03 PM EDT
Location: /dev/ph
Posts: 4,996
Thanks: 73
Thanked 475 Times in 437 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 Unix and Linux 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 Unix and Linux 10-21-2010
durden_tyler's Unix or Linux Image
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
Join Date: Apr 2009
Last Activity: 9 September 2017, 1:30 PM EDT
Posts: 2,083
Thanks: 21
Thanked 383 Times in 346 Posts
Alternatively, you could use Perl -

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

Sponsored Links
Old Unix and Linux 10-21-2010
methyl methyl is offline Forum Advisor  
Join Date: Mar 2008
Last Activity: 8 June 2016, 9:58 PM EDT
Posts: 6,402
Thanks: 288
Thanked 676 Times in 645 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 Unix and Linux 10-21-2010
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline Forum Staff  
Join Date: Feb 2007
Last Activity: 5 May 2017, 7:24 AM EDT
Location: The Netherlands
Posts: 7,746
Thanks: 139
Thanked 558 Times in 520 Posts

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

Sponsored Links

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

All times are GMT -4. The time now is 01:25 AM.