Computing.Net > Forums > Unix > sed and uppercase one char

sed and uppercase one char

Reply to Message Icon

Original Message
Name: Juuso
Date: July 27, 2006 at 00:29:23 Pacific
Subject: sed and uppercase one char
OS: SunOS 5.9
CPU/Ram: ?
Model/Manufacturer: ?
Comment:

I have XML-file that has lines like:
...
<record>
<field id='data1'>something</field>
<field id='data2'>111-11a</field>
</record>
...

How can I convert the alphabetical character in data2 to uppercase? And convert only that one letter in each data2 (there are about 16 000 lines of data2 and the alphabetical character can be anything between a to z...)? Can I use sed? My knowledge is not enough... Can someone help me?


Report Offensive Message For Removal


Response Number 1
Name: Juuso
Date: July 27, 2006 at 03:32:42 Pacific
Subject: sed and uppercase one char
Reply: (edit)

I found the solution myself, so I place it in here if it would help someone else:

# convert one letter to uppercase
# The code modified from Loukides, Mike; Peek, Jerry; Powers, Shelley: The Unix Power Tools, 3rd edition
/[>][0-9]\{3\}-[0-9]\{2\}[a-z][<]/{
h
s/.*[>][0-9]\{2\}-[0-9]\{2\}\(.\)[<].*/\1/
y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
G
s/\(.*\)\n\(.*[>][0-9]\{3\}-[0-9]\{2\}\).\([<].*\)/\2\1\3/
}

- I saved the file as the name bigletter
- Usage: sed -f bigletter filename.xml > filename2.xml

and the explanation:

# address limits to lines that are like >111-11a<
/[>][0-9]\{3\}-[0-9]\{2\}[a-z][<]/{

# copy the current input line into the hold space
h

# gets the small letter from the line
# I use the special character . (one any character) because the address limits the right lines
s/.*[>][0-9]\{2\}-[0-9]\{2\}\(.\)[<].*/\1/

# change the lowercase letter to uppercase letter
y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/

# G (Get) appends the line saved in the hold space to the pattern space
G

# substitute command makes the thing and puts the uppercase letter to the right place!
s/\(.*\)\n\(.*[>][0-9]\{3\}-[0-9]\{2\}\).\([<].*\)/\2\1\3/
}

And the job was done...


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: sed and uppercase one char

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge