Visit Our UNIX and Linux User Community


Changing the character after the Underscore(_)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Changing the character after the Underscore(_)
# 1  
Old 03-21-2012
Changing the character after the Underscore(_)

Hi Everyone,

I am looking for a command that would do the following:

1) Change all the letters/words in a file to Lower case Letters/words.
2) Remove the Underscore (_) and Change the Character after the underscore (_) to an Uppercase letter.

Example:

Code:
File contains the below words:

FILTER_WORD
USA_NEWJERSEY
USA_NEWYORK_NYC

Output should be:
filterWord
usaNewjersey
usaNewyorkNyc

It would be really helpful if someone could help me out achieving this.

Thanks a lot for all your time!!!
# 2  
Old 03-21-2012
Code:
$ perl  -alne '$_=~s/_/ /g; $_=~s/(\w+)/\u\L$1/g;$_=~s/ //g;print lcfirst $_' input.txt
filterWord
usaNewjersey
usaNewyorkNyc

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 03-21-2012
Code:
perl -pe '$_=lc;while(/_([a-z])/g){$x=$1;$y=uc($x);s/_$x/$y/g}' inputfile

This User Gave Thanks to balajesuri For This Post:
# 4  
Old 03-21-2012
awk

Hi,

Here is the awk solution,

Code:
 awk '{$0=tolower($0);while(match($0,"_")){l=substr($0,match($0,"_"),2);L=toupper(l);sub(l,L);sub(/_/,"");}}1' file


Cheers,
RangaSmilie
This User Gave Thanks to rangarasan For This Post:
# 5  
Old 03-21-2012
Thank you very much for all your quick replies.

Its really helpful.
# 6  
Old 03-21-2012
Shorter:
Code:
perl -pe '$_=lc;s/_([a-z])/\u$1/g' inputfile

This User Gave Thanks to balajesuri For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #51
Difficulty: Easy
The term 'IoT' means the 'Internet of Tomorrow'.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To count underscore in filename

Hi All I have a filename like below adsf_ghjt_kop_pol.csv Can you please get me a command for counting the number of underscore in the filename. In the above filename I should get output as 3, since the file name is having three underscore (5 Replies)
Discussion started by: ginrkf
5 Replies

2. HP-UX

sed help with underscore problems

Hello, I have spent a couple of hours trying to answer this myself, so forgive me if the answer is simple but I have tried. I have a text file generated from svn log output which contains a list of files. Two regexps im using are * and * They both work but some lines has a mixture... (7 Replies)
Discussion started by: YogaBija
7 Replies

3. Programming

Changing double to character string in C++

I want to convert an double to a character string. I done the integer and float ones using itoa and ftoa. How can I do a similar thing with doubles? String::String ( const float f ) { char *cdata = ftoa (f); strcpy (STR, cdata.c_str ()); } String::String ( const double ... (1 Reply)
Discussion started by: kristinu
1 Replies

4. Shell Programming and Scripting

Changing paragraph to a width of 80 character

I have the following text file and want to change the paragraphs so that they fill 80 characters rather than the usual 65 in this case. Is there an easy way to do this? All three groups were distinguished by the remarkable manner in which they moved about on the solar surface, not only in... (7 Replies)
Discussion started by: kristinu
7 Replies

5. Shell Programming and Scripting

underscore to dots

Hi, I have been trying to change underscores to dots. For example: 1122_91 1022_233 . 2237_23 9382_2339 2998_234 345_257 . . Desired output: 1122.91 1022.233 . 2237.23 9382.2339 2998.234 345.257 . . Any idea? Thanks (4 Replies)
Discussion started by: iconig
4 Replies

6. Shell Programming and Scripting

Insert underscore at certain places

Hi all, I have to insert underscore at certain places(places before and after PAxxx/PAxxxx entries in a big file like this ESR1 PA156 leflunomide PA450192 CHST3 PA26503 docetaxel tungstate Pa4586; thalidomide Pa34958; PAxxx/PAxxxx entries are metioned between 2 names in each row ... (4 Replies)
Discussion started by: manigrover
4 Replies

7. UNIX for Advanced & Expert Users

google search changes with an underscore

How does adding an underscore change a google search? I was searching for John Elway and got different results with and without an unscore. (3 Replies)
Discussion started by: cokedude
3 Replies

8. Shell Programming and Scripting

allow only alphnumeric entry and an underscore

I am taking an user input which should only be an alphanumeric character or an underscore. How to i do it? (2 Replies)
Discussion started by: vickylife
2 Replies

9. Shell Programming and Scripting

usage of underscore in awk

Hi what is the purpose of using underscore in awk. I suppose it is for defining macro's and reducing repeatation but can some one show me an example? (6 Replies)
Discussion started by: ahmedwaseem2000
6 Replies

10. UNIX for Dummies Questions & Answers

problem with underscore in variable name

can you help please. variable 1 = TODAY=`date +"%Y%m%d"` i.e. echo $TODAY 20080407 DB=GERMANY echo $DB GERMANY echo $DB.$TODAY GERMANY.20080407 echo $DB.$TODAY_1.dmp GERMANY..dmp (3 Replies)
Discussion started by: pinkie
3 Replies

Featured Tech Videos