How to determine if a file is ASCII?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to determine if a file is ASCII?
# 8  
Old 02-05-2002
Did I miss something here?

I must have missed something in the question and answers.
Why not use the file utility ?

Quote:
NAME
file - determine file type

SYNOPSIS
file [ -bcnsvzL ] [ -f namefile ] [ -m magicfiles ] file
...

DESCRIPTION

[deleted many lines ....]

If an argument appears to be an ASCII file, file attempts
to guess its language. The language tests look for par-
ticular strings (cf names.h) that can appear anywhere in
the first few blocks of a file. For example, the keyword
.br indicates that the file is most likely a troff(1)
input file, just as the keyword struct indicates a C pro-
gram. These tests are less reliable than the previous two
groups, so they are performed last. The language test
routines also test for some miscellany (such as tar(1)
archives) and determine whether an unknown file should be
labelled as `ascii text' or `data'.
# 9  
Old 02-05-2002
I have tried the "file" utility. I agree with popo that it falls down when used in a script. The problem is that it doesn't just divide the universe into text and binary files. Rather it outputs stuff like:
Code:
shared object file
lif file
core file
relocatable object file
lint library
compiled Lisp
c program text
s800 shared executable dynamically linked -not stripped

That is just some of the output that I got from a "file *". In many cases it is wrong. But even if were all correct, think what this would do to my script. I would need a case statement with hundreds of entries to collapse this into just "text" and "binary". That would result in a longer, slower script and what's worse, it would no longer be portable. The "file" command on Solaris doesn't know about s800 executables.

In most cases, you don't want a program to guess what language a script is written in. You just want to know if you can safely grep a string out of it or something.
# 10  
Old 02-05-2002
OK, Perderabo, thanks. Now I understand. However, isn't there a simple case (wrapper) switch that can be used to place the output of file into either a ASCII or binary category? Or, as you suggest, file basically flawed and it is better not avoid all together?

# 11  
Old 02-05-2002
Quote:
Originally posted by Neo
However, isn't there a simple case (wrapper) switch that can be used to place the output of file into either a ASCII or binary category?
In the real olden days, all text files happened to result something like "c program text", "commands text", etc. And so you could check for "*text*" and it would pretty much work. But this subtlety never was documented on the man page. Too many vendors have made too many extentions to "file" for it to be useful today.

On HP-UX, *text*|FrameMaker*|postscript*|shar* would be needed to handle my home directory. I'm not sure if would handle everything. And I have no faith that it would work with the next release of HP-UX. The FrameMaker stuff bothers me. I don't know what FrameMaker is, but I doubt that any of my files would be sensible input to FrameMaker.

Besides, once shells became powerful enough to classify a file with only built-in commands, it made sense to switch anyway. Scripts that do everything without using external programs run much faster.
# 12  
Old 02-05-2002
Thanks, much appreciated.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Hex to Ascii in a Ascii file

Hi All, I have an ascii file in which few columns are having hex values which i need to convert into ascii. Kindly suggest me what command can be used in unix shell scripting? Thanks in Advance (2 Replies)
Discussion started by: HemaV
2 Replies

2. UNIX for Dummies Questions & Answers

After Ftp'ing file to destination how to check the file if it is in correct ASCII and not corrupted

Hi Folks, While transferring file from FTP software like Filezilla the files gets corrupted. Is there any way I can check if the recently transferred file is in ASCII and not corrupted. I have tried using file -i filename command which does tell if the file character set is ASCII or binary... (6 Replies)
Discussion started by: Khan28
6 Replies

3. Shell Programming and Scripting

How to determine if there's a file in directory!

Hi All, I'm just wondering how can i determined if there's a file in directory and put it in a logs? dir="/home/test/" Please advise, Thanks, Use code tags, thanks. (1 Reply)
Discussion started by: nikki1200
1 Replies

4. Shell Programming and Scripting

Determine BL and RL of a file

A regular ebcdic mainframe tape usually contains header information the 1st three blocks of the tape. The header information tells the computer/user more information about what is on tape. The header info is 240 bytes in length at 80 bytes each header. The 1st block/header is volume name or... (1 Reply)
Discussion started by: Linux-wannabe
1 Replies

5. Shell Programming and Scripting

convert ascii values into ascii characters

Hi gurus, I have a file in unix with ascii values. I need to convert all the ascii values in the file to ascii characters. File contains nearly 20000 records with ascii values. (10 Replies)
Discussion started by: sandeeppvk
10 Replies

6. UNIX for Advanced & Expert Users

How to determine if a file is done copying

I have a file repository in a directory where files are copied into it by ftp or samba. Some of the ftp transfers can be slow, and some of the files can be fairly large. The files are not being used for anything in this directory other than being taken out of the directory and used by the... (2 Replies)
Discussion started by: husker_ricky
2 Replies

7. Shell Programming and Scripting

determine owner of a file

Hello, I am on a mission to determine the user of file. I have used the ls -l command but it displays permission, link, user, group, etc, but I just want to display just the name of user of a specified file. Many thanks (4 Replies)
Discussion started by: unibboy
4 Replies

8. Shell Programming and Scripting

ftp - determine ascii or binary file

Hello, How to i determine via ftp commandline if files on ftp server is ascii or binary files. Like every other comon windows ftp program does it automatically. regards Thomas (5 Replies)
Discussion started by: congo
5 Replies

9. UNIX for Dummies Questions & Answers

determine the size of a file???

Hello, Can someone please tell me which command to use to determine the size of a file? When I log in to my shell account, I do this $>% ls -als total 632 8 -rw-r--r-- 1 user01 devgrp1 1558 Jul 30 23:25 .kshrc What is "1158"? Bytes? Kilobytes? I apologize if my... (8 Replies)
Discussion started by: alan
8 Replies

10. UNIX for Advanced & Expert Users

How to determine if a File is Open?

I need to know what the best way, if possible in a perl or shell script, to determine if a file is open by a process, and if it is open for writing. While I would rather use a perl or shell script, if I have to use C, that would be ok. Thanks. (2 Replies)
Discussion started by: derrikw2
2 Replies
Login or Register to Ask a Question