Operating System - HP-UX
1758605 Members
2475 Online
108873 Solutions
New Discussion юеВ

sed batch editor - Change select lowercase areas from lower to upper case

 
SOLVED
Go to solution
Mr Peter Kempner
Occasional Contributor

sed batch editor - Change select lowercase areas from lower to upper case

I have many small unix text files that I am transforming using sed. There is one thing I cannot achieve:

change each occurence of $IING$xx to $IING_XX
where xx can be any 2 letters. ie I want to tell sed to change to uppercase the 2 letters following the RE pattern of '$IING$'

Does anyone have any ideas. I think it may involve the use of the holding space???
4 REPLIES 4
Arthur Pols
Advisor

Re: sed batch editor - Change select lowercase areas from lower to upper case

Dear Peter,

I don't use "sed" to do this.
Maybe this will help you;

VAR_CAP=`echo ${VAR_xx} | tr '[a-z]' '[a-z]'`

This will translate all characters (form a to z) into capitals.

Good luck,

Arthur Pols
The purpose of education is to replace an empty mind with an open one.
RikTytgat
Honored Contributor

Re: sed batch editor - Change select lowercase areas from lower to upper case

Hi,

I do not see a straightforward solution using sed, but if you have perl 5 on your system, followinf script does the job:

========
#!/opt/perl5/bin/perl -w

open(INPUT, "/tmp/testfile");

while ( ) {
if ( /^(.*)\$IING\$(..)(.*)$/ ) {
$upper = uc($2);
print "$1", '$IING$', "$upper", "$3", "\n";
} else {
print;
}
}
=======


Hope this helps,
Rik
federico_3
Honored Contributor

Re: sed batch editor - Change select lowercase areas from lower to upper case

you should do like this:

VAR_CAP=`echo ${VAR_xx} | tr '[a-z]' '[A-Z]'`

federico

Curtis Larson
Trusted Contributor
Solution

Re: sed batch editor - Change select lowercase areas from lower to upper case

if you want to use sed:

/.*\$IING\$...*/ {
h
s/.*\$IING\$\(..\).*/\1/
y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
G
s/\(.*\)\n\(.*\$IING\$\)..\(.*\)/\2\1\3/
}

But that will only change the first occurance.

for every occurance you'll need something along these lines, which is written in awk:

/\$IING\$../ { for ( i=1;i<=NF;i++ ) {
if ( i != 1 ) printf(" ");

if ( $i ~ /\$IING\$../ ) {
s=toupper(substr($i,7));
printf("$IING$%s",s);
}
else { printf("%s",$i);}
}
}

But this will remove any special line formatting (white space) that there might be.