Solved

Need a script to edit many files

Posted on 2002-03-25
11
235 Views
Last Modified: 2010-04-21
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
0
Comment
Question by:mboss
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 4

Expert Comment

by:newmang
ID: 6895376
Do you mean you want to edit the file contents or do you mean you want to rename the files themselves?
0
 

Author Comment

by:mboss
ID: 6895450
I want to edit the file contents
0
 
LVL 38

Expert Comment

by:yuzh
ID: 6895497
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

0
 

Author Comment

by:mboss
ID: 6896237
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
0
 

Author Comment

by:mboss
ID: 6896263
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
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:mboss
ID: 6896264
that should be...

Actaully I think I know why it DIDN'T work

0
 
LVL 38

Expert Comment

by:yuzh
ID: 6896273
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

 

0
 

Author Comment

by:mboss
ID: 6896309
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

0
 
LVL 4

Expert Comment

by:newmang
ID: 6897735
What about a quick and dirty 'C' program to accomplish the same thing?
0
 
LVL 5

Expert Comment

by:ecw
ID: 6897984
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).
0
 
LVL 38

Accepted Solution

by:
yuzh earned 250 total points
ID: 6898169
This one should add a newline to the file and do the job,

#!/bin/ksh
for i in `cat $1` ; do
  cat $i | awk '{print $0,"\n"}' >$i.tmp
  sed 's/LABQDV/LAB/' $i.tmp > $i

  # If you need to delete the newline chart, comment
  # out the following 2 lines
  # tr -d "\012" < $i > ${i}.tmp
  # mv ${i}.tmp $i
 
  rm ${i}.tmp
done





0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now