Solved

Need a script to edit many files

Posted on 2002-03-25
11
236 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

863 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

23 Experts available now in Live!

Get 1:1 Help Now