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.

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

Shell Programming and Scripting


Tags
convert unix windows

Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
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
    #2  
Old Unix and Linux 10-20-2010
fpmurphy's Unix or Linux Image
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 19 April 2015, 3:24 AM EDT
Location: /dev/ph
Posts: 4,910
Thanks: 68
Thanked 453 Times in 421 Posts
Unfortunately you cannot do it that way.

From SED oneliners
Quote:
# 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
    #3  
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.
    #4  
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: 5 April 2015, 4:04 PM EDT
Posts: 1,904
Thanks: 9
Thanked 285 Times in 259 Posts
Alternatively, you could use Perl -


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

tyler_durden
Sponsored Links
    #5  
Old Unix and Linux 10-21-2010
methyl methyl is offline Forum Advisor  
Advisor
 
Join Date: Mar 2008
Last Activity: 14 April 2015, 8:02 PM EDT
Posts: 6,398
Thanks: 288
Thanked 675 Times in 644 Posts
Quote:
sed s/$/^M/ ~/unix.txt ~/dos.txt
You can generate the carriage return character in Shell:

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

Sponsored Links
    #6  
Old Unix and Linux 10-21-2010
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 18 April 2015, 9:26 AM EDT
Location: The Netherlands
Posts: 7,730
Thanks: 128
Thanked 544 Times in 512 Posts

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

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image 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 04:18 AM.