|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
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
|
|
#3
|
|||
|
|||
|
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} |
|
#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... |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Help please - Processing all files in a directory |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|