unix, find and replace

I want to find and replace one string, finding is tricky for example if there are inputs values like below,
 boks_hjimn_cat, boks_hmm_dog, boks_go_rawst

I should replace boks_ begin values with addition value appended, but boks_ begin values and rawst end values should not be replaced. The output should be like this in the replaced file.
Db.boks_hjimn_cat,
Db.boks_hmm_dog,
boks_go_rawst.

I just gave example of 3 values but this file more than 200 pattern of this type.
Please let me know if there is a combination of find, awk or sed commands.
welcome 123Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TintinCommented:
Your description is unclear.

Are you saying any string that starts with boks_ and ends with rawst should NOT be replaced and all other variations should?
0
savoneCommented:
This would probably work, although you might end up with a comma at then end of the last line.  Not sure if that is a deal breaker or not. It would also be a lot simpler if you knew how many value there were in the CSV file.  This code is assuming you are using a file named pat.csv (which I tested on), please change that to match your needs.

#!/bin/bash
FIELD=1
COUNT=`sed 's/[^,]//g' pat.csv | wc -c`; let "COUNT+=1"

while [ "$FIELD" -lt "$COUNT" ]; do 
	CONTENT=`cat pat.csv | cut -d, -f$FIELD`
	if [[ "$CONTENT" == *rawst* ]]; then
		echo "$CONTENT,"
		let "FIELD+=1"
		continue
	else
		echo "Db.$CONTENT,"
		let "FIELD+=1"
	fi
done

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
welcome 123Author Commented:
Actually my input file is a txt file with multiple sql queries. This file has words like boks_hjimn_cat, boks_hmm_dog, boks_go_rawst etc in random postions. I need to find the words beginning with boks_ but the word should not end with _rawst and append Db. in the beginning to those words.

so, boks_hjimn_cat should be replaced as Db.boks_hjimn_cat,  
boks_hmm_dog should be replaced as Db.boks_hmm_dog
but the word boks_go_rawst should remain same boks_go_rawst .

If I use sed\"boks_"\"Db.boks_"\ then all the words are replaced, even the one ending with rawst,
so can you suggest how to do that?
This can be either perl script or unix script or sed or awk command, find, replace  etc.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

savoneCommented:
Can you give us an example of the file? It's hard to visualize what your saying.  Are they all on one line?  Each on their own line? etc...
0
welcome 123Author Commented:
Even using find and replace with vi editor is also fine with you suggest me the correct expression.
0
welcome 123Author Commented:
The file is  a  perl module having several functions. In some of the functions the script is using select * from boks_go_rawst  or set .boks_hjimn_cat = 1; or update boks_hmm_dog etc.
0
welcome 123Author Commented:
OK, wrote this script and it should probably work but this one is replacing all tabs and extra spaces in my original file to single space.

#!/usr/bin/perl -w

use strict;
use IO::File; #for filehandle
use File::Basename;
use File::Copy;
use Data::Dumper;

my $File = "C:\\scripts\\input.pm";
my $WriteFile = "C:\\scripts\\input1.pm";
my @newfile;

      if (open (FILE, "< $File" )) {
            my @secdata = <FILE>;
            close FILE;
            foreach my $secline (@secdata) {
                  chomp $secline;
                  my @line = split (/\s/, $secline);
                  my @newline;
                  foreach my $word (@line) {
                        chomp $word;
                        if(($word =~ /boks_/) && ($word !~ /rawst/)) {
                              $word = "Dat." . $word;
                        }
                        push (@newline, $word);
                  }
                  push (@newline, "\n");
                  push (@newfile, join(' ', @newline));
                      
            }
            
      }
      else {
            print "Cannot open the File $File for reading $!\n"
      }
      
      print Data::Dumper->Dump([\@newfile], [qw(*newfile)]);
       if ( open(LOG, ">$WriteFile"))   {
          print LOG @newfile;
        close LOG;
      }
      else {
            print "Cannot open the File $WriteFile for writing$!\n"
      }
0
welcome 123Author Commented:
Finally the below solution seems to be working.

#!/usr/bin/perl -w

use strict;
use IO::File; #for filehandle
use File::Basename;
use File::Copy;
use Data::Dumper;

my $File = "C:\\scripts\\input.pm";
my $WriteFile = "C:\\scripts\\input1.pm";
my @newfile;
if (open (FILE, "< $File" )) {
      my @secdata = <FILE>;
      close FILE;
      foreach my $secline (@secdata) {
            chomp $secline;
            my @line = split (/ /, $secline);
            my @newline;
            foreach my $word (@line) {
                  chomp $word;
                  if(($word =~ /boks_/) && ($word !~ /rawst/)) {
                        $word = "Dat." . $word;
                  }
                  push (@newline, $word);
            }
            my $newlinestr = join(' ', @newline);
            $newlinestr=~s/\s+$//g;
            $newlinestr .= "\n";
            push (@newfile, $newlinestr);
                  
      }
            
}
else {
      print "Cannot open the File $File for reading $!\n"
}
      
#print Data::Dumper->Dump([\@newfile], [qw(*newfile)]);
if ( open(LOG, ">$WriteFile"))   {
      print LOG @newfile;
      close LOG;
}
else {
      print "Cannot open the File $WriteFile for writing$!\n"
}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.