Solved

Need a script to edit many files

Posted on 2002-03-25
11
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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.
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.

687 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