ucfirst() with "_" separator

G

gors

We have big file has lines as:

ITEM1 PRODUCT PRICE 3.47
ITEM2 PRODUCT_DETAILS PRICE 4.47
ITEM3 PRODUCT_DETAILS_AGAIN PRICE 5.47

Trouble is make 2nd word each line capital letter first, then lower case for
every "_". Some 2nd word have "_" one or more time, other not. So we like
make line each lokk like:

ITEM1 Product PRICE 3.47
ITEM2 Product_Details PRICE 4.47
ITEM3 Product_Details_Again PRICE 5.47

How operate on second word after each "_"? We try:

echo "ITEM3 PRODUCT_DETAILS_AGAIN PRICE 5.47" | \
perl -aF_ -ne '$stuff=ucfirst(lc $2); s/$2/$stuff/; print $_'

but it not work make any change. Even:

echo "PRODUCT_DETAILS_AGAIN" | \
perl -aF_ -ne 'print ucfirst(lc $_);'

produce only "Product_details_again" instead "Product_Details_Again".

Someone can help please?
 
U

Uri Guttman

g> ITEM3 PRODUCT_DETAILS_AGAIN PRICE 5.47

g> Trouble is make 2nd word each line capital letter first, then lower
g> case for every "_". Some 2nd word have "_" one or more time, other
g> not. So we like make line each lokk like:

g> ITEM3 Product_Details_Again PRICE 5.47

g> How operate on second word after each "_"? We try:

g> echo "ITEM3 PRODUCT_DETAILS_AGAIN PRICE 5.47" | \
g> perl -aF_ -ne '$stuff=ucfirst(lc $2); s/$2/$stuff/; print $_'

you aren't doing it for all the occurances of _. and that is not a good
way to do it anyhow.

read perldoc -q capital to see how it is done with words separated by
blanks. you should be able to modify that to use _. if you need more
help then show the new code here.

uri
 
J

John W. Krahn

gors said:
We have big file has lines as:

ITEM1 PRODUCT PRICE 3.47
ITEM2 PRODUCT_DETAILS PRICE 4.47
ITEM3 PRODUCT_DETAILS_AGAIN PRICE 5.47

Trouble is make 2nd word each line capital letter first, then lower case for
every "_". Some 2nd word have "_" one or more time, other not. So we like
make line each lokk like:

ITEM1 Product PRICE 3.47
ITEM2 Product_Details PRICE 4.47
ITEM3 Product_Details_Again PRICE 5.47

How operate on second word after each "_"? We try:

echo "ITEM3 PRODUCT_DETAILS_AGAIN PRICE 5.47" | \
perl -aF_ -ne '$stuff=ucfirst(lc $2); s/$2/$stuff/; print $_'

but it not work make any change. Even:

echo "PRODUCT_DETAILS_AGAIN" | \
perl -aF_ -ne 'print ucfirst(lc $_);'

produce only "Product_details_again" instead "Product_Details_Again".

Someone can help please?

$ echo "ITEM1 PRODUCT PRICE 3.47
ITEM2 PRODUCT_DETAILS PRICE 4.47
ITEM3 PRODUCT_DETAILS_AGAIN PRICE 5.47" | \

perl -pe's/^(\S+\s+)(\S+)/$a = $1; ($b = $2) =~ s!([[:alpha:]]+)!\L\u$1!g;
"$a$b"/e'
ITEM1 Product PRICE 3.47
ITEM2 Product_Details PRICE 4.47
ITEM3 Product_Details_Again PRICE 5.47



John
 
A

anno4000

gors said:
We have big file has lines as:

ITEM1 PRODUCT PRICE 3.47
ITEM2 PRODUCT_DETAILS PRICE 4.47
ITEM3 PRODUCT_DETAILS_AGAIN PRICE 5.47

Trouble is make 2nd word each line capital letter first, then lower case for
every "_". Some 2nd word have "_" one or more time, other not. So we like
make line each lokk like:

ITEM1 Product PRICE 3.47
ITEM2 Product_Details PRICE 4.47
ITEM3 Product_Details_Again PRICE 5.47

How operate on second word after each "_"? We try:

echo "ITEM3 PRODUCT_DETAILS_AGAIN PRICE 5.47" | \
perl -aF_ -ne '$stuff=ucfirst(lc $2); s/$2/$stuff/; print $_'

but it not work make any change. Even:

echo "PRODUCT_DETAILS_AGAIN" | \
perl -aF_ -ne 'print ucfirst(lc $_);'

produce only "Product_details_again" instead "Product_Details_Again".

while ( <DATA> ) {
my @words = split;
$_ = join '_', map ucfirst, split /_/, lc for $words[ 1];
print "@words\n";
}

__DATA__
ITEM1 PRODUCT PRICE 3.47
ITEM2 PRODUCT_DETAILS PRICE 4.47
ITEM3 PRODUCT_DETAILS_AGAIN PRICE 5.47

Anno
 
U

Uri Guttman

MD> On Thu, 22 Feb 2007 12:14:51 +0100, Michele Dondi

MD> Well, of course there's a missing bit:

MD> s|(?<=\s)(\w+)(?=\s)|join '_', map ucfirst(lc), split /_/, $1|e;

MD> What's worst, (I tested the former and) it took me a while to
MD> understand what was wrong... :-(

MD> of course a slight modification a' la Anno's suggestion in this same
MD> thread is better:

MD> 's|(?<=\s)(\w+)(?=\s)|join '_', map ucfirst, split /_/, lc $1|e;

i like this approach. it seems the term always starts with PRODUCT so i
assumed that. i sent the 3 lines from the OP into this one liner and it
works. not often you get to see nested s/// ops. :)


perl -lpe 's/(PROD\w+)/$x = $1 ; $x =~ s{([A-Z]+)}{\u\L$1}g ; $x/e'
ITEM1 PRODUCT PRICE 3.47
ITEM1 Product PRICE 3.47
ITEM2 PRODUCT_DETAILS PRICE 4.47
ITEM2 Product_Details PRICE 4.47
ITEM3 PRODUCT_DETAILS_AGAIN PRICE 5.47
ITEM3 Product_Details_Again PRICE 5.47

uri
 
D

Dr.Ruud

Michele Dondi schreef:
s|(?<=\s)(\w+)(?=\s)|join '_', map ucfirst, split /_/, lc $1|e;


Shorter:

s|(?<=\s)(\w+)|join '_', map ucfirst, split /_/, lc $1|e;

s|(?<= )(\w+)|join _=>map ucfirst,split _=>lc$1|e;

Alternative approach:

local $";
s|(?<=\s)(\w+)|@{[map ucfirst, split /(_)/, lc $1]}|;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top