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
Solved

Need a script to edit many files

Posted on 2002-03-25
11
239 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ovirt 3.6 guest VM support oracle solaris 4 81
Linux: disable vim auto-comment 7 136
Install MySQL 5.6 and PHP on Centos Linux 6 136
Linux hostname change 2 83
In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
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…
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.

792 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