mboss
asked on
Need a script to edit many files
I have many files all of the same structure. They all begine with
MSH|^~\&|LABQDV|QDV|CMX|.. .......
I need a script that will edit all the files in a given directory and change "LABQDV" to "LAB"
so my end result will be files that start with
MSH|^~\&|LAB|QDV|CMX|..... ..
Thanks
mboss
MSH|^~\&|LABQDV|QDV|CMX|..
I need a script that will edit all the files in a given directory and change "LABQDV" to "LAB"
so my end result will be files that start with
MSH|^~\&|LAB|QDV|CMX|.....
Thanks
mboss
Do you mean you want to edit the file contents or do you mean you want to rename the files themselves?
ASKER
I want to edit the file contents
The following little script should do the job for you.
Put all the filenames in a text file FILELIST (with path)
#!/bin/ksh
for i in `cat $1` ; do
sed 's/LABQDV/LAB/' $i > $i.$$
mv $i.$$ $i
done
save the above script as chfile, chmod u+x
to use it:
./chfile FILELIST
You can modify the script or just use command line to do
the job.
eg. you want to change all the *.txt in dir1,
cd dir1
for i in `ls *.txt` ; do
sed 's/LABQDV/LAB/' $i > $i.$$
mv $i.$$ $i
done
cheers!
=============
yuzh
Put all the filenames in a text file FILELIST (with path)
#!/bin/ksh
for i in `cat $1` ; do
sed 's/LABQDV/LAB/' $i > $i.$$
mv $i.$$ $i
done
save the above script as chfile, chmod u+x
to use it:
./chfile FILELIST
You can modify the script or just use command line to do
the job.
eg. you want to change all the *.txt in dir1,
cd dir1
for i in `ls *.txt` ; do
sed 's/LABQDV/LAB/' $i > $i.$$
mv $i.$$ $i
done
cheers!
=============
yuzh
ASKER
All my test files are a size of zero now. I cut and pasted what you had
here's what I have
#!/bin/ksh
for i in `cat $1` ; do
sed 's/LABQDV/LAB/' $i > $i.$$
mv $i.$$ $i
done
here's what I have
#!/bin/ksh
for i in `cat $1` ; do
sed 's/LABQDV/LAB/' $i > $i.$$
mv $i.$$ $i
done
ASKER
Actaully I think I know why it did work.
All my files, when opened up in vi, have incomplete last line. S each file is not terminated by a new line character.
Is there a way around this.
The man page for sed says it ignores lines that aren't terminated by a new line character.
Thanks
mboss
All my files, when opened up in vi, have incomplete last line. S each file is not terminated by a new line character.
Is there a way around this.
The man page for sed says it ignores lines that aren't terminated by a new line character.
Thanks
mboss
ASKER
that should be...
Actaully I think I know why it DIDN'T work
Actaully I think I know why it DIDN'T work
Can you please tell me which favour of UNIX are you using, and the contains of you FILELIST.
The little works find under Solaris 7/8 (SPARC)
The to do the followings :
#!/bin/ksh
for i in `cat $1` ; do
sed 's/LABQDV/LAB/' $i > ${i}.tmp
mv ${i}.tmp $i
done
The little works find under Solaris 7/8 (SPARC)
The to do the followings :
#!/bin/ksh
for i in `cat $1` ; do
sed 's/LABQDV/LAB/' $i > ${i}.tmp
mv ${i}.tmp $i
done
ASKER
The problem is that all my files are one long line that isn't ended with a new line character. sed doesn't read a line in that isn't terminated by a new line character.
Is there a way around this problem?
I'm on HPUX 11
Is there a way around this problem?
I'm on HPUX 11
What about a quick and dirty 'C' program to accomplish the same thing?
quick and dirt shell ...
#!/usr/bin/ksh
for i in "$@" ; do
( cat "$i" ; echo ) | sed 's/LABQDV/LAB/' > ".$i.t" &&
mv "$i" ".$i.o" && ln ".$i.t" "$i" && rm -f ".$i.t"
done
This will end up putting a newline after the last line read, which may leave a blank line at the end of each file.
It should leave the originals file intact but renamed (use ls -a).
#!/usr/bin/ksh
for i in "$@" ; do
( cat "$i" ; echo ) | sed 's/LABQDV/LAB/' > ".$i.t" &&
mv "$i" ".$i.o" && ln ".$i.t" "$i" && rm -f ".$i.t"
done
This will end up putting a newline after the last line read, which may leave a blank line at the end of each file.
It should leave the originals file intact but renamed (use ls -a).
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.