LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-07-2008, 06:50 PM   #1
mikesjays
Member
 
Registered: Dec 2005
Distribution: FC8, FC9, FC10
Posts: 30

Rep: Reputation: 15
How to convert Line endings of text files


I have been working my way trough a book on shell scripts. Cool book so far. I started to write a script with VI in a terminal on my my Mac Book. I then scp'd it to my linux box and finished writing it in VI on my Linux box. When I run it I get the error "line 45: syntax error: unexpected end of file" My script ends on line 44. All I can think of is the line endings are different between Mac and Linux and that is what is wrong. I know that there is dos2linux for dos to linux file conversions. Is there any thing for mac to linux?
 
Old 04-07-2008, 07:16 PM   #2
beadyallen
Member
 
Registered: Mar 2008
Location: UK
Distribution: Fedora, Gentoo
Posts: 209

Rep: Reputation: 36
Can you post the script. It's far more likely you've missed a closing quote somewhere, and the shell hasn't found it by the end. AFAIK MAc and other Unix have the same line endings. Maybe it's worth a try with dos2unix anyway,
 
Old 04-07-2008, 07:25 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Nope, he's right.

Linux use LF
Dos uses CR/LF
Mac uses CR


Code:
tr '\015' '\012' < file.mac > file.unix
should do the trick ...


Cheers,
Tink

Last edited by Tinkster; 04-07-2008 at 07:29 PM. Reason: twisted paws
 
Old 04-07-2008, 07:30 PM   #4
mikesjays
Member
 
Registered: Dec 2005
Distribution: FC8, FC9, FC10
Posts: 30

Original Poster
Rep: Reputation: 15
I will go ahead and post the script but you were right. if and fi, damn dyslexia gets me every time. It was the last fi that I typed as an if. Thank you any way.
#!/bin/sh
# normdate -- Normalizes month field in date specification
# to three letters, first letter capitalized. A helper
# function for the script #7, valid-date. Exits w/ zero if no error.

monthnoToName()
{
# Sets the variable 'month' to the appropriate value
case $1 in
1 ) month="Jan" ;; 2 ) month="Feb" ;;
3 ) month="Mar" ;; 4 ) month="Apr" ;;
5 ) month="May" ;; 6 ) month="Jun" ;;
7 ) month="Jul" ;; 8 ) month="Aug" ;;
9 ) month="Sep" ;; 10) month="Oct" ;;
11) month="Nov" ;; 12) month="Dec" ;;
* ) echo "$0: Unknown numeric month value $1" >&2; exit 1

esac
return 0
}

## Begin main script

if [ $# -ne 3 ] ; then
echo "usage: $0 month day year" >&2
echo "Typical input formats are August 3 1962 and 8 3 2002" >&2
exit 1
fi

if [ $3 -le 99 ] ; then
echo "$0: expected four-digit year value." >&2; exit 1
fi

if [ -z $(echo $1|sed 's/[[:digit:]]//g' ) ] ; then
monthnoToName $1
else
# normalize to first three letters, first upper, rest lowercase
month="$(echo $1|cut -c1|tr '[:lower:]' '[:upper:]')"
month="$month$(echo $1|cut -c2-3 | tr '[:upper:]' '[:lower:]')"
fi

echo $month $2 $3

exit 0
 
Old 04-07-2008, 07:55 PM   #5
mikesjays
Member
 
Registered: Dec 2005
Distribution: FC8, FC9, FC10
Posts: 30

Original Poster
Rep: Reputation: 15
Tinkster,

Can you explain what this code is doing? more exact the '\015' '\012'

thanks
Quote:
Originally Posted by Tinkster View Post
Nope, he's right.

Linux use LF
Dos uses CR/LF
Mac uses CR


Code:
tr '\015' '\012' < file.mac > file.unix
should do the trick ...


Cheers,
Tink
 
Old 04-07-2008, 10:43 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by mikesjays View Post
Tinkster,

Can you explain what this code is doing? more exact the '\015' '\012'

thanks
Read "man ascii" ;} and search for the strings 015 012 ...

tr replaces any occurrence of the first in the input
stream with the second.


Cheers,
Tink

P.S. Can you do me a favour and don't use top posting when
quoting? Ta.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert DOS text files to UNIX text files ta0kira Linux - Software 7 03-15-2011 11:42 AM
Steps needed to convert multiple text files into one master text file jamtech Programming 5 10-07-2007 11:24 PM
Application to convert MySQL files MYD, MYI, FRM to SQL text files ? SethShannon Linux - Newbie 1 09-28-2007 06:03 PM
need to convert text files into mysql database zafar466 Linux - Software 1 08-28-2007 12:53 AM
Convert text files to html files lothario Linux - Software 3 09-27-2005 08:48 PM


All times are GMT -5. The time now is 06:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration