Operating System - HP-UX
1752505 Members
5392 Online
108788 Solutions
New Discussion юеВ

check file format (creation dos or unix)

 
SOLVED
Go to solution
Billa-User
Regular Advisor

check file format (creation dos or unix)

hello,

what is the best way to detect, if file was created in DOS with ^M .

two options:
grep -c '^M$' file

od -c file |grep '\\r \\n'

regards
8 REPLIES 8
Matti_Kurkela
Honored Contributor

Re: check file format (creation dos or unix)

If you're handling text files that may or may not have been created in DOS/Windows, another possibility is to simply always pipe the file through dos2ux.

If the file is already in unix format, then HP-UX dos2ux won't make any conversion at all.

So you can do things like:
dos2ux unknown.txt > definitely-unix.txt

The ux2dos command works the same way.

(See for yourself: "ux2dos .profile | ux2dos | ux2dos | od -c" won't naively cause multiple \r's to be added: after the first ux2dos, the subsequent ones detect the line terminators are already in DOS form and will do nothing.)

MK
MK
Billa-User
Regular Advisor

Re: check file format (creation dos or unix)

hello,

@ "ux2dos .profile | ux2dos | ux2dos | od -c"

i know "ux2dos" , but i don't want to convert a file. we have an application, with only allows developed programs in unix format like shell scripts,etc...

so i want to check for "^M" and if exists , i report an error.

regards
James R. Ferguson
Acclaimed Contributor
Solution

Re: check file format (creation dos or unix)

Hi:

Using 'grep' with the keyboard or 'vi' sequence to generate a carriage-return character is one way.

Using 'grep' with '\r' is not guaranteed to work in all Unix or Linux environments.

I would do something like:

# perl -nle 'BEGIN{$n=0};$n++ if m{\r$}g;END{print $n}' file

...to count the number of carriage returns.

...or better, if I wanted to rid my file of any:

# perl -pi -e 's{\r$}{}' file

Strictly speaking, a DOS file also ends with ^Z so to eliminate that too:

# perl -pi -e s{\r$}{};s{^\032$}{}' file

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: check file format (creation dos or unix)

You could do the following. (That's "^" followed by "M", not control M.)
cat -v file | fgrep -q "^M"
if [ $? -eq 0 ]; then
echo "DOS format for file"
fi

Or:
$ fgrep $(echo "\r\c") file

echo supports "\r" and other escapes.
Steven Schweda
Honored Contributor

Re: check file format (creation dos or unix)

> [...] we have an application, [...]

> [...] so i want to check [...]

I'm confused. Do you want to know how to
check a file using some shell-script code, or
do you want to add code to "an application"
(about which we non-psychics know nothing),
or what?
Billa-User
Regular Advisor

Re: check file format (creation dos or unix)

hello steven,

i want to check with a script, if a file includes ^M . Which is for my meaning 100 % created with DOS ( in DOS editor).

Dennis or James posted a solution for me.

regards
Viktor Balogh
Honored Contributor

Re: check file format (creation dos or unix)

> so i want to check for "^M" and if exists , i report an error.

what if you would just convert the input with dos2ux to a reliable format, no matter of what type it is. So you won't need to display that error message, and your application would have one more feature.
****
Unix operates with beer.
James R. Ferguson
Acclaimed Contributor

Re: check file format (creation dos or unix)

Hi:

The potential pitfall of relying on 'dos2ux' is one of portability. You won't find this (except?) on HP-UX.

Caveat emptor.

Regards!

...JRF...