Check if file is EBCDIC or ASCII format

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Check if file is EBCDIC or ASCII format
# 1  
Old 01-24-2018
Check if file is EBCDIC or ASCII format

So, i have this requirement where i need to check the file format, whether it's EBCDIC or ASCII, and based on format retrieve the information from that file:

my file is:

file1.txt-->this ebcdic file
file2.txt-->ascii file

i tried below code:
Code:
file=file1.txt
type="`file $file`"

i get output if file is ebcdic:
Code:
file1.txt: data

if file is ascii:
Code:
file2.txt: ASCII text, with no line terminators

now my requirement is i need to do some operations if file is ebcdic and some if file is ascii:

Code:
type="`file $file`"
if grep -q data "$type"; then
echo "ebcdic"
else
echo "ascii"
fi

output:
Code:
grep: 010020001_S-FOR-Sort-SYEXC_20180109_062320_0100.x937: data: No such file or directory
ascii

firstly its not searching the word data correctly in $type variable and its showing something like no such file or directory, can anyone guide me where i am going wrong, and also what will the best approach for this requirement.
TIA
# 2  
Old 01-24-2018
Hi,

I think I can see where you might have gone a little bit wrong here. I assume that when you run:

Code:
grep -q data "$type"

you're attempting to see if the string "data" occurs in the contents of the variable "$type", which will indeed be correctly populated with the output of the file command. However, that's not what grep does. The grep command specifically searches the contents of files, and nothing else.

So what's going wrong here is that grep is trying to search for the string "data" in a file on disk in the current directory called (e.g. in the case of an ASCII file) "test: ASCII text", which does not exist.

If you want to check for the contents of a variable rather than a file, there's a few ways you could do that. Probably one of the easiest changes for you to make in your script would be to do a test like this instead:

Code:
if echo "$type" | grep "data"

This would cause grep to treat sandard input as its file, into which we are piping the output of the echo "$type" command. So this would ultimately check to see if the variable "$type" contained the string "data", which is I think what you're wanting to do.

Hope this helps.
# 3  
Old 01-24-2018
grep takes a file for input, not the contents of a shell variable. To analyse a shell variable, you can use the test (or [ ) or case command, or - in more recent shells - the [[ compound command. For example (please be aware that my file command outputs different results for ebcdic):

Code:
case ${type#*: } in
        Non-ISO*)       echo "file type: ebcdic" ;;
        ASCII*)         echo "file type: ASCII" ;;
        *)              echo "file type not recognised." ;;
esac

or
Code:
[[ "$type" =~ Non-ISO ]] && echo ebcdic || echo ASCII

# 4  
Old 01-31-2018
It would help to know the source of this data--is it mostly letters and numbers or are there a lot of special characters. If you have very few characters (like Greek letters, graphics, or math symbols) higher than x'7f', you and assume it is ASCII. In EBCDIC alphabetic and numeric characters are all higher than that. Also, a space in ASCII is x'20' while an EBCDIC space is x'40', the ASCII symbol for the at sign "@". Perhaps compare the number of at signs to the file size?
This table: Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion is a good place to start.
# 5  
Old 01-31-2018
Also, by EBCDIC do happen to mean "packed decimal"?
# 6  
Old 01-31-2018
Packed decimal has little to do with EBCDIC or ASCII. Each byte contains two decimal digits except the last byte may contain a sign like x'C' for positive, x'D' for negative, and x'F' for unsigned.
In COBOL, a signed five digit field would be represented with
Code:
77  FIELD-NAME   PIC S9(5) COMP-3.

and would occupy three bytes. In hex, the number -12345 would be x'12 34 5D'.
# 7  
Old 01-31-2018
Quote:
Originally Posted by wbport
Packed decimal has little to do with EBCDIC or ASCII.
I know that. People asking about EBCDIC here usually don't, though, and almost always are looking into packed decimal instead. Time will tell.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Convert EBCDIC(.DAT) FILE into ASCII FILE

Hi Team, I am having 100 EBCDIC files (i.e. DAT extension) and need to convert them into ASCII File by unix shell script. I tried with DD Command but its not providing output as expected. Sample Text: ------------------ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Expected Output:... (2 Replies)
Discussion started by: JSM
2 Replies

2. Programming

Ebcdic to ascii

Hi, I want to convert ebcdic values to ascii values. Are there anyany specific c++ libraries with g++ compiler, which can do it ? gcc version 4.1.2 20080704 (Red Hat 4.1.2-54) (19 Replies)
Discussion started by: tostay2003
19 Replies

3. Red Hat

Unable to convert EBCDIC file to ASCII file

Hello all, To give you all a little bit of background. We recently migrated from HP-UX to Redhat Linux and one of the command I used to run on HP-UX to convert an EBCDIC file to ASCII file isn't working on Linux. The code is as follow: cat workout2.dat | dd cbs=250 conv=block conv=ascii... (3 Replies)
Discussion started by: sethmj
3 Replies

4. Shell Programming and Scripting

EBCDIC Format to ASCII

Hi, we have source file with EBCDIC format(Main Frame files) where we receving from source system. I would like to convert the EBCDIC format file to unix systemformat(ex: .csv,txt ) I have wrote script like: dd if=<SRCPATH>yyy.xxx.RB065 of=<SRCPATH>/output.csv ibs=800 cbs=80... (8 Replies)
Discussion started by: koti_rama
8 Replies

5. UNIX for Dummies Questions & Answers

Help! EBCDIC format to ASCII

Hi everyone, I have a 70MB EBCDIC file, with record length 102, block size 32742 and IBM standard label. I commanded dd if=input file of=outputfie ibs=32742 cbs=102 conv=ascii but I still don't get a viewable file under ASCII. Can anyone told me what's the problem? Do I need... (12 Replies)
Discussion started by: hrchl86
12 Replies

6. Shell Programming and Scripting

How to check if the file has EBCDIC or ascii characters

Hi, is there a way to check if the initial few characters are ebcdic or ascii in a file? (1 Reply)
Discussion started by: ahmedwaseem2000
1 Replies

7. UNIX for Dummies Questions & Answers

How to convert ebcdic file to ascii file?

How to convert ebcdic file to ascii file? (1 Reply)
Discussion started by: superuser123
1 Replies

8. Shell Programming and Scripting

How to convert ebcdic file to ascii file?

How to convert ebcdic file to ascii file? (1 Reply)
Discussion started by: superuser123
1 Replies

9. UNIX for Dummies Questions & Answers

To convert multi format file to a readable ascii format

Hi I have a file which has ascii , binary, binary decimal coded,decimal & hexadecimal data with lot of special characters (like ..ݡ.ݡ ) in it. I want to standardize the file into ASCII format & later use that as source . Can any one suggest a way a logic to convert such... (5 Replies)
Discussion started by: gaur.deepti
5 Replies

10. Shell Programming and Scripting

Check whether a given file is in ASCII format and data is tab-delimited

Hi All, Please help me out with a script which checks whether a given file say abc.txt is in ASCII format and data is tab-delimited. If the condition doesn't satisfy then it should generate error code "100" for file not in ASCII format and "105" if it is not in tab-delimited format. If the... (9 Replies)
Discussion started by: Mandab
9 Replies
Login or Register to Ask a Question