how to identify the mode of the file in unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to identify the mode of the file in unix
# 1  
Old 02-10-2009
how to identify the mode of the file in unix

hi,
I have a requirement in that i need to process a input file
The problem is, the input file sometimes it is coming in dos mode and some times it is coming in unix mode
The script which i have written will process the file only if it is in unix mode and it is not processing if the file is in dos mode
Is there any method to check whether the input file is in dos mode or in unix mode within the script itself (while processing)
So that i can handle the same in my script

Thanks in advance

Regards,
Selvaraj S
# 2  
Old 02-10-2009
convert file to unix

first convert the windows file to unix format using

tr -d '\r' < infile > outfile

or

dos2unix infile > outfile

then process.
# 3  
Old 02-10-2009
Selva,

How exactly is the file coming to the server? Is it through FTP? If so, what mode are you using for transferring? ASCII or BINARY?

Note that if the FTP transfer mode is ASCII, then the files might have ^M characters or square boxes appended at the end of each line, which may result in the failure of your script. Hence, BINARY mode of FTP transfer is always preferred.

HTH, Smilie

Regards,

Praveen
# 4  
Old 02-11-2009
hi,
i am not asking for tr -d '\r' < infile > outfile

if you are inside shell script, from the script itself you can check whether a file is having execute, read, write permissions

like that i am asking. From within a shell script i want to check whether the input file is in dos mode or unix mode

My intention is in whatever format (dos or unix) they send the file to unix box my script has to check the type and if it is not matching it has to convert it into unix mode with in the shell script while processing



thanks
# 5  
Old 02-11-2009
try this

Code:
#!/bin/ksh

# to type ^M, keeping ctrl pressed, press vm

grep "^M" $1


RETURN_CODE=$?
echo $RETURN_CODE

if [ $RETURN_CODE -eq 0 ]; then
        echo "DOS File"
else
        echo "Unix File"
fi

# 6  
Old 02-12-2009
Hi,
Let me explain the problem


The below few lines are from one file called retst.out
I just copied few of the lines by opening the file in vi mode
you can see the [dos] in the lines below (in vi editor)

Input file in dos mode
------------------------

2008110803000200050000011 10.0000 10.0000
2008111503000200050000011 10.0000 10.0000
2008112203000200050000011 10.0000 10.0000
2008112903000200050000011 10.0000 10.0000

"retst.out" [dos] 52L, 3016C


Same file after converting it into unix mode

2008110803000200050000011 10.0000 10.0000
2008111503000200050000011 10.0000 10.0000
2008112203000200050000011 10.0000 10.0000
2008112903000200050000011 10.0000 10.0000
"retst.out" 52L, 2964C



I will use the below lines for converting my file from dos mode to unix mode

tr -d '\15\32' < retst.out > 1.txt
mv 1.txt retst.out


If you see the file above I am seeing any ctl+M (^M) characters in the file
This conversion I am asking.

How to do the same

thanks
# 7  
Old 02-12-2009
Just for clarification: the term "mode" in UNIX usually refers to the permission bits set (read/write/execute). What you mean would be the line termination style (DOS/Windows: \r\n, UNIX: \n, Apple: \r) or the file encoding.
To convert a file from DOS style to UNIX style, the commands suggested by rakeshou should work just fine.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to identify varying unique fields values from a text file in UNIX?

Hi, I have a huge unsorted text file. We wanted to identify the unique field values in a line and consider those fields as a primary key for a table in upstream system. Basically, the process or script should fetch the values from each line that are unique compared to the rest of the lines in... (13 Replies)
Discussion started by: manikandan23
13 Replies

2. Shell Programming and Scripting

Open a file in UNIX mode

hi, The function open(OUTPUT, ">$output_filename"); #opens the file in (DOS) windows mode. how to open the output file in unix mode. please suggest. thanks. (10 Replies)
Discussion started by: Rashid Khan
10 Replies

3. Shell Programming and Scripting

How to identify whether the script is in Unix format or not ?

Hi All, I have the below scenario in my environment Developers used to copy file from windows to Linux box. Some time on the copied file developers miss to run the dos2unix utility. Because of this script gets failed during the execution. Most of the failures are due to the dos2unix format... (7 Replies)
Discussion started by: kalpeer
7 Replies

4. AIX

How to Identify long running unix processes

Hi All, Need an urgent help, I have a requirement to find long running unix processes.. I have tried the below commands, but not succeed. I need to arrange the unix processess in an order of elapsed time (high to low) that runs in a system. For Eg: Consider we have 3 processes, Pid 1 pid 2... (5 Replies)
Discussion started by: mohamedirfan
5 Replies

5. UNIX for Dummies Questions & Answers

Identify issues through logging in unix

I am a newbie to unix and appeared for an interview and was asked this question. If i was working for a company as a support person on an application and has to monitor the logs how would i determine if there was outage by checking the logs and where would i search for the issue initially. and... (2 Replies)
Discussion started by: praveenveerla
2 Replies

6. Programming

Unix system crashing - Need help to identify the issue

Hi, We are accessing our remote webserver using libcurl.We are using GET method to post the request.The request is a simplified URL.We will receive XML Response for the URL request. Problem For Few Scenarios, response for the URL request is received & our component is killed abruptly. Can... (11 Replies)
Discussion started by: banus
11 Replies

7. Shell Programming and Scripting

Identify high values "ÿ" in a text file using Unix command

I have high values (such as ÿÿÿÿ) in a text file contained in an Unix AIX server. I need to identify all the records which are having these high values and also get the position/column number in the record structure if possible. Is there any Unix command by which this can be done to : 1.... (5 Replies)
Discussion started by: devina
5 Replies

8. UNIX for Dummies Questions & Answers

identify a customer under a UNIX server

Like many UNIX users, there is a small team that is connected to UNIX (under the same UNIX user) via putty from a Windows PC. Is there a way, once under UNIX, to know the address of the originating computer or anything that can differentiate initial users ? (7 Replies)
Discussion started by: popescu1954
7 Replies

9. UNIX for Advanced & Expert Users

Identify a remote machine as windows or unix

Hi, I have a IP address of the target machine.Is there is any way to find out whether it is a unix box or windows box without logging into it?. Regs Anand (5 Replies)
Discussion started by: u449064
5 Replies

10. UNIX for Dummies Questions & Answers

how does unix identify C and other language code!

Hello everyone, this question is probably simple for you ppl but i am jus a beginner and still learing. i was wondering if someone could tell me that how does unix identify different program codes in it's environment. for example, unix recognises this C code in the body of any script. how... (2 Replies)
Discussion started by: a25khan
2 Replies
Login or Register to Ask a Question