Hi,
I have a file like this:
mgr1.dbf
tool.dbf
usr.dbf
wow19.dbf
wow2wow.dbf
Can anyone help change the format after each line of reading to:
mgr2.dbf # add 1 of *1.dbf
tool2.dbf # if not a number exist, make it default to 2
usr2.dbf
wow20.dbf # add 1 of 19.dbf
wow3wow.dbf # add 1 of *1 .dbf
thank you for the help!
That's a pretty interesting one!
perl looks like the most appropriate choice for this:
#!/usr/bin/perl -w
while (<>) {
if (/^([^\d\.]*)(\d*)([^\d.]*)\.([^\.]+)$/) {
if ($1) { print "$1" }
if ($2) {
$val=$2+1;
print $val;
$suffix="";
} else {
$suffix="2";
}
if ($3) { print "$3" }
if ($suffix) { print "$suffix" }
print ".$4";
} else {
print "# Could not parse the following line:\n";
print "$_";
}
}
It works great!
but is there a way that I can use korn shell to accomplish it?
That's a pretty interesting one!
perl looks like the most appropriate choice for this:
#!/usr/bin/perl -w
while (<>) {
if (/^([^\d\.]*)(\d*)([^\d.]*)\.([^\.]+)$/) {
if ($1) { print "$1" }
if ($2) {
$val=$2+1;
print $val;
$suffix="";
} else {
$suffix="2";
}
if ($3) { print "$3" }
if ($suffix) { print "$suffix" }
print ".$4";
} else {
print "# Could not parse the following line:\n";
print "$_";
}
}
here is a test result:
stzhao:pts/7:sambar:dman > cutstring.pl test.txt
12rgm.log
mgr2.dbf
tool2.dbf
usr2.dbf
wow20.dbf
wow3wow.dbf
# Could not parse the following line:
11number12.dbf
# Could not parse the following line:
char14between13.dbf
# Could not parse the following line:
# Could not parse the following line:
Sorry, adding more test condition, if two or more numbers appearing in the string, can we increase the last number?
like:
11number12.dbf --> 11number13.dbf
char14between13.dbf --> char14between14.dbf
rikxik
December 4, 2007, 2:39am
5
Well, I appreciate the urge to "just do it" in ksh - so here goes:
Input:
Script:
Result:
Obviously this is quite brittle but for the kind of input provided, this should do.
Note: done using default ksh on SunOS db012a 5.8 Generic_117350-35 sun4us sparc FJSV,GPUZC-M
HTH
rikxik:
Well, I appreciate the urge to "just do it" in ksh - so here goes:
Input:
Script:
Result:
Obviously this is quite brittle but for the kind of input provided, this should do.
Note: done using default ksh on SunOS db012a 5.8 Generic_117350-35 sun4us sparc FJSV,GPUZC-M
HTH
it works for most of creteria so I would say it is good enough, I will add some more features to it as well
Thank you all for the help!