Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search and Replace 2

Status
Not open for further replies.

Popak

MIS
Oct 25, 2005
11
US
Hello,

I am not a programmer and I just need to replace some chracters. Here is the story: our db exports this file that includes name, last name and some dates, Due to user error, date fields are including - instead of /. would you please kind enough to tell me where I can find these codes!!!! and here is the tricky part some of the last names are having hyphonated.

thank you so very much for the help.

-Popak
 
Can you post some example data?
Before and after would be ideal. How it is now and how you want it to look.
Sounds like a very simple task.


Trojan.
 
ok here is some an example:
"800000000","john","smith","8-27-2005","5-18-2006"
"800000001","li","lee-lee","8/27/2005","5/18/2006"

I would like the / replces - but not effecting the last name, in above example I would like lee-lee stays like that. so it would like:

"800000000","john","smith","8/27/2005","5/18/2006"
"800000001","li","lee-lee","8/27/2005","5/18/2006"

thank you,

-Popak
 
You might want to try this:
Code:
#!/usr/bin/perl -w
use strict;
while(<>) {
  chomp;
  my @fields = split /","/,$_,-1;
  $fields[3] =~ tr[-][/];
  $fields[4] =~ tr[-][/];
  print join('","', @fields), "\n";
}
Feed it the data file on the command line and it'll output the corrected data on STDOUT.


Trojan.
 
You can actually do all of this with a one-liner:
Code:
perl -p -i.bak -e 's:(\d{2})-(\d{2})-(\d{4}):$1/$2/$3:' [i]input_file[/i]

-e introduces a script given as the next argument - quoted to protect it form the shell.

-i says to keep a backup of the original file (with a .bak extension)

-p wraps a loop around your code, effectively saying "do this to every line"

The code itself looks for the distinctive pattern of dates and changes the minus characters to slashes. I've used colons to delimit the substitution (s:::) rather than the more common s/// so that I don't have to escape the literal backslashes in the replacement pattern. Note that you could also change the order of date elements this way if you wished: $1, $2 and $3 correspond to the three sets of parentheses on the left and could appear in any order.

Yours,

fish

[&quot;]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.[&quot;]
--Maur
 
guys:

thank you so very much, you rock.

-Popak
 
Hi

another quick questions: how can i combine this search and replacement s/\cM/\n/g with the script that you guys gave that to me:
CODE
#!/usr/bin/perl -w
use strict;
while(<>) {
chomp;
my @fields = split /","/,$_,-1;
$fields[3] =~ tr[-][/];
$fields[4] =~ tr[-][/];
print join('","', @fields), "\n";
}
I would like to remove this ^m from the lines and replace that with new line?

thanks again.

-Popak
 
Adding 1, in the first two ()'s on Fish's line will catch dates like "8-5-2005" and the trailing 'g' will make it change both dates.

perl -p -i.bak
-e 's:(\d{1,2})-(\d{1,2})-(\d{4}):$1/$2/$3:g' input_file

Adding s/\x0d/a\n/ to that:

perl -pi.bak
-e 's:(\d{1,2})-(\d{1,2})-(\d{4}):$1/$2/$3:g;
s:\x0d:\n:g' input_file

removes the ^M's

Kordaff
 
Perldoc perlre will work from a c:\ prompt iirc as well as [bash:~] etc =)

Kordaff
 
kordaff,

but unfortunalty I don't know why fish'es script doesn't work with my file (the output is empty) although trojan script works fine, but still trying to figure out how to remove ^M and integrade that with trojan's script.
how can i combine this search and replacement s/\cM/\n/g with the script that you guys gave that to me:
CODE
#!/usr/bin/perl -w
use strict;
while(<>) {
chomp;
my @fields = split /","/,$_,-1;
$fields[3] =~ tr[-][/];
$fields[4] =~ tr[-][/];
print join('","', @fields), "\n";
}


thanks again.

-Popak
 
I'm taking a guess that you want the sequence of caret followed by 'M' replaced by a newline only in field 3 and 4:

#!/usr/bin/perl -w
use strict;
while(<>) {
chomp;
my @fields = split /","/,$_,-1;
$fields[3] =~ s<\^M><\n>g; # If you don't escape the caret, it becomes a regex token for 'beginning of line'
$fields[4] =~ s<\^M><\n>g;
print join('","', @fields), "\n";
}

__END__

hope it helps!


----

In need of programming book recommendations.
 
Try like this:

Code:
#!/usr/bin/perl -w
use strict;
while(<>) 
  {
  s:(\d{1,2})-(\d{1,2})-(\d{4}):$1/$2/$3:g;
  s:\x0d:\n:g;    # this is to change ^M's
  print $_;
  }

Kordaff
 
The ^M is actually not a literal ^ and M, it's chr(13), the carriage return which displays as ^M in some editors and not at all in others. For Unix line endings you need it to be the \n or chr(10, the linefeed.

\x0d is the string to match hexidecimal value 0d (or 13 decimal) in a regular expression.

Kordaff
 
A *nix newbie says thanks :) I take you can also replace it with

s|\r|\n|g;

for that matter.


----

In need of programming book recommendations.
 
Hmm, yeah, guess I was thinking \r didn't for some reason lol. TIMTOWTDI =)

Kordaff <--was making web based ftp example plh.org/ftp.pl
 
thank you ... thank you. I start kind of liking it, works perfect.

-Popak
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top