The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Identify IP address of a printer Queue in HP-Unix lenin1985 HP-UX 2 06-11-2008 04:21 AM
Identify records having junk characters in unix sureshg_sampat Shell Programming and Scripting 6 11-14-2007 02:44 PM
Identify a remote machine as windows or unix u449064 UNIX for Advanced & Expert Users 5 08-05-2006 01:03 PM
Identify type of file DebianJ Shell Programming and Scripting 1 11-17-2005 11:55 AM
how does unix identify C and other language code! a25khan UNIX for Dummies Questions & Answers 2 01-21-2004 10:44 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-10-2009
trichyselva trichyselva is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 65
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 (permalink)  
Old 02-10-2009
rakeshou rakeshou is offline
Registered User
  
 

Join Date: May 2007
Posts: 75
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 (permalink)  
Old 02-10-2009
sunpraveen's Avatar
sunpraveen sunpraveen is offline
Registered User
  
 

Join Date: Jan 2009
Location: Pruthvi
Posts: 85
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,

Regards,

Praveen
  #4 (permalink)  
Old 02-11-2009
trichyselva trichyselva is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 65
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 (permalink)  
Old 02-11-2009
rakeshou rakeshou is offline
Registered User
  
 

Join Date: May 2007
Posts: 75
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 (permalink)  
Old 02-12-2009
trichyselva trichyselva is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 65
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 (permalink)  
Old 02-12-2009
pludi's Avatar
pludi pludi is online now Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,947
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:04 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0