LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to convert Line endings of text files (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-convert-line-endings-of-text-files-633781/)

mikesjays 04-07-2008 06:50 PM

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?

beadyallen 04-07-2008 07:16 PM

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,

Tinkster 04-07-2008 07:25 PM

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

mikesjays 04-07-2008 07:30 PM

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

mikesjays 04-07-2008 07:55 PM

Tinkster,

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

thanks
Quote:

Originally Posted by Tinkster (Post 3114019)
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


Tinkster 04-07-2008 10:43 PM

Quote:

Originally Posted by mikesjays (Post 3114034)
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.


All times are GMT -5. The time now is 04:58 AM.