insert "_" before uppercase letter with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting insert "_" before uppercase letter with awk
# 1  
Old 04-06-2011
insert "_" before uppercase letter with awk

Hi,

I am trying to insert a "_" character in front of capital letters in a string. For instance:

Code:
HappyDaysAreHereAgain

needs to become
Code:
Happy_Days_Are_Here_Again

I know how to do this with sed, but I am writing an awk script, which does several things. Also, I have already set FS in order to extract my title case string. I was thinking something like this, but it doesn't match on [A-Z].

Code:
BEGIN{FS="."}
...
{
    name=$2":"$3
    gsub("[A-Z]", "_&", name);
    print name;
}
...

Any ideas?
Cheers,
Kato
# 2  
Old 04-06-2011
Try:
Code:
gsub ("[[:upper:]]", "_&",name);
sub("^_","",name)

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 04-06-2011
Thanks Bartus, that works fine, I forgot about those char classes. Any idea why [A-Z] didn't match?
# 4  
Old 04-06-2011
Code:
sed 's/[A-Z]/_&/g;s/^_//' infile

This User Gave Thanks to ctsgnb For This Post:
# 5  
Old 04-06-2011
Quote:
Originally Posted by kato
Thanks Bartus, that works fine, I forgot about those char classes. Any idea why [A-Z] didn't match?
No idea. It is weird for me too.
# 6  
Old 04-06-2011
Quote:
Originally Posted by bartus11
No idea. It is weird for me too.
Works fine with Solaris' "nawk".
# 7  
Old 04-06-2011
Quote:
Originally Posted by vgersh99
Works fine with Solaris' "nawk".
I tested on Linux Smilie
Code:
$ awk --version
GNU Awk 3.1.5
Copyright (C) 1989, 1991-2005 Free Software Foundation.
...

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

awk separate field by letter and ";"

i have a 2 fields in my DB ID25333,1429291340lNormPUC-AP_MEX_UFM-GOL_44;PUC-AP_VEX_UFM-ROL_55;PUCAP_MEX_UFM-DOJ_49; ID55555,1429291340lNormPUC-AP_PPP_UFM-HOL_44;PUC-AF_GEX_UJM-SOL_45;PUCAP_MEX_UFM-DOJ_59;and i need separate like this ID25333,PUC-AP_MEX_UFM-GOL_44; ... (5 Replies)
Discussion started by: Axl_north
5 Replies

3. Shell Programming and Scripting

awk if then "insert"

I have this output: FA-7H 5001438004c224b8 5001438005692b18 5001438005694778 500143800569491c 50014380056952e0 c0507604cc5c00a2 FA-10H 5001438004c224ba 5001438005692b1a 5001438005694372 500143800569477a 50014380056952e2 c0507604cc5c00a0 I would like this: FA-7H ... (2 Replies)
Discussion started by: elilmal
2 Replies

4. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

7. Shell Programming and Scripting

help for saving vertical datas to horizontal with "awk" or "cut"

hi, i have a file having datas like that ./a.txt 12344 12345 12346 12347 ..... ..... ... i want to save this datas to another file like that ./b.txt 12344 12345 12346 12347 ... ... ... i think awk can make this but how? :) waiting for ur help. (3 Replies)
Discussion started by: mercury
3 Replies

8. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question