UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old November 1st, 2004, 03:02 AM
PriyaR PriyaR is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 1 PriyaR User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Help please - Processing all files in a directory

I need to open all files starting with M in a directory, go line by line and remove all EOL characters(^M). The rest should remain the same.
How to write a script for this? Any help will be appreciated.

Reply With Quote
  #2  
Old November 1st, 2004, 08:16 AM
andyb1ack andyb1ack is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 60 andyb1ack User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 14 sec
Reputation Power: 4
Make sure you take a backup of all your files first!
It's so easy to make a mistake and lose all your files.
I've done it (once)!

Run it once and do some checks.... maybe using "diff" although I'm not sure what it'll do with control characters...

Note that you get a ^M by holding the control key and then typing "v" then "m" (still holding the control key down).

Code:
DIR=/some/directory/containing/files

for FILE in `find ${DIR} -type f -name "M*" ! -name "*.processed"`
do
  echo Processing file ${FILE}
  sed 's/^M//' ${FILE} > ${FILE}.processed

  #Unhash the line below to rename the files back
  #mv ${FILE}.processed ${FILE}  
done

Reply With Quote
  #3  
Old November 2nd, 2004, 04:32 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,083 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 49 m 57 sec
Reputation Power: 9
my version (supposed you are on *nix)

Code:
dir=....
for FILE in `find $DIR -type f -name "M*"`
do dos2unix $FILE $FILE
done


andy: a tip (not an indoctrination)
all the {} IN THIS EXEMPLE are useless and confusing
${abc}.qqq and $abc.qqq are equivalent, not
${abc}_qqq and $abc_qqq
the point is:
always using {} (i repeat, it's NOT an error) is just confusing
are, are not needed ?
in: a=${b:-qqq} you get probls if $b is ${b}

Reply With Quote
  #4  
Old November 2nd, 2004, 08:15 AM
andyb1ack andyb1ack is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 60 andyb1ack User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 14 sec
Reputation Power: 4
Hi guggach,

No probs. I'm always up for tips.

I've always used the ${FILE} syntax rather than $FILE syntax:
- I can easily use sed on a script to change ${FILE} to ${FILENAME} without worrying about changing, say, $FILED ...
- It's not obvious to all when reading a script that if FILE=bob then $FILENAME is not bobNAME

I'll probably still stick with it even though it's a little more typing... I find it clearer, but could be just what I'm used to...

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Help please - Processing all files in a directory


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway