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


Tags
convert unix windows

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
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
    #2  
Old 10-20-2010
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 18 December 2014, 8:26 PM EST
Location: /dev/ph
Posts: 4,842
Thanks: 64
Thanked 434 Times in 402 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 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 10-21-2010
durden_tyler's Avatar
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
 
Join Date: Apr 2009
Last Activity: 11 December 2014, 1:49 PM EST
Posts: 1,856
Thanks: 7
Thanked 269 Times in 244 Posts
Alternatively, you could use Perl -


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

tyler_durden
Sponsored Links
    #5  
Old 10-21-2010
methyl methyl is offline Forum Advisor  
Advisor
 
Join Date: Mar 2008
Last Activity: 25 November 2014, 5:09 PM EST
Posts: 6,396
Thanks: 287
Thanked 673 Times in 642 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 10-21-2010
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 17 December 2014, 7:05 AM EST
Location: The Netherlands
Posts: 7,720
Thanks: 126
Thanked 538 Times in 508 Posts

Code:
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 05:21 AM.