?
Solved

Need a script to edit many files

Posted on 2002-03-25
11
Medium Priority
?
244 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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
 

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 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses

850 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