You can limit which files match
Also, if you use bash you can enable the extglob option. Then you don't need sed at all.
Code:
#!/bin/bash
DESTDIR=thetargetdirectory
shopt -s extglob
# create the target directory if it doesn't exits
if [ ! -d "${DESTDIR}" ]; then mkdir "${DESTDIR}"; fi
# for every file with an _m0*.txt suffix, remove leading zeros.
for i in *_m+(0)*.txt ; do
cp $i "${DESTDIR}"/${i/_m+(0)/_m}
done
If you have tens of thousands of files matching this pattern, you will need to use the find command to produce the filelist instead of file globbing or ls.
If you have a more complicated pattern to match, you could use sed to provide the initial file list:
Code:
for i in $(ls *.txt | sed -n '/<pattern>/p'); do
...
Sometimes, having a complicated pattern to match, you want to either use echo before the "cp" command to debug your script, or instead of executing commands, use sed to construct a script.
I did something similar yesterday where I used a directory list of your MVL server, extracted spot numbers into a list (with "cut").
Using cut, i got a list of spot numbers from a range of schedules. Then I produce another list of spot numbers from the adtec units and produced two files of just spot numbers.
Code:
comm -13 <(sort -u scheduledspots) <(sort inventory) | sed 's/.*/DEL &.MPG/' >ftpscript
A oneliner produced an ftp script to delete files in the units that weren't in the schedules.
I could then look at this script and proof it to make sure I wasn't deleting scheduled files.