Convert text between exact matching patterns to Title case


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert text between exact matching patterns to Title case
# 1  
Old 02-10-2016
Question Convert text between exact matching patterns to Title case

Hi Folks,

I have a large text file with multiple similar patterns on each line like:
Code:
blank">PATTERN1 some word PATTERN2
title=">PATTERN1 some word PATTERN2
blank">PATTERN1 another word PATTERN2
title=">PATTERN1 another word PATTERN2
blank">PATTERN1 one more time PATTERN2
title=">PATTERN1 one more time PATTERN2

I would like to convert all occurrences between title=">PATTERN1 and PATTERN2 to Title Case format.

Desired output:
Code:
blank">PATTERN1 some word PATTERN2
title=">PATTERN1 Some Word PATTERN2
blank">PATTERN1 another word PATTERN2
title=">PATTERN1 Another Word PATTERN2
blank">PATTERN1 one more time PATTERN2
title=">PATTERN1 One More Time PATTERN2

Any idea on accomplishing this is much appreciated. Thanks very much for your help!

Last edited by martinsmith; 02-10-2016 at 07:47 PM..
# 2  
Old 02-10-2016
Code:
cat patterns.file

Code:
blank">PATTERN1 some word PATTERN2
title=">PATTERN1 some word PATTERN2
blank">PATTERN1 another word PATTERN2
title=">PATTERN1 another word PATTERN2 and leave this alone PATTERN2
blank">PATTERN1 one more time PATTERN2
title=">PATTERN1 one more time but not last PATTERN2

Please, give it a try:

Code:
perl -ple '/title=">PATTERN1\s+(.*?)\s+PATTERN2/ and $s=$1 and s|$s|join " ", (map {ucfirst} split /\s/, $s)|e' patterns.file

Code:
blank">PATTERN1 some word PATTERN2
title=">PATTERN1 Some Word PATTERN2
blank">PATTERN1 another word PATTERN2
title=">PATTERN1 Another Word PATTERN2 and leave this alone PATTERN2
blank">PATTERN1 one more time PATTERN2
title=">PATTERN1 One More Time But Not Last PATTERN2


Last edited by Aia; 02-10-2016 at 09:05 PM..
This User Gave Thanks to Aia For This Post:
# 3  
Old 02-10-2016
Quote:
Originally Posted by Aia

Please, give it a try:

Code:
perl -ple '/title=">PATTERN1\s+(.*?)\s+PATTERN2/ and $s=$1 and s|$s|join " ", (map {ucfirst} split /\s/, $s)|e' patterns.file

[CODE]
Hi Aia, It works perfectly as i was hoping for! Thank you very much for this solution. Smilie
# 4  
Old 02-11-2016
Quote:
Originally Posted by martinsmith
Hi Folks,

I have a large text file with multiple similar patterns on each line like:
Code:
blank">PATTERN1 some word PATTERN2
title=">PATTERN1 some word PATTERN2
blank">PATTERN1 another word PATTERN2
title=">PATTERN1 another word PATTERN2
blank">PATTERN1 one more time PATTERN2
title=">PATTERN1 one more time PATTERN2

I would like to convert all occurrences between title=">PATTERN1 and PATTERN2 to Title Case format.
Desired output:
Code:
blank">PATTERN1 some word PATTERN2
title=">PATTERN1 Some Word PATTERN2
blank">PATTERN1 another word PATTERN2
title=">PATTERN1 Another Word PATTERN2
blank">PATTERN1 one more time PATTERN2
title=">PATTERN1 One More Time PATTERN2

Any idea on accomplishing this is much appreciated. Thanks very much for your help!
Hello martinsmith,

Following awk solution may help you too in same, let me know if this helps.
Code:
awk '/^title=\">PATTERN1.*PATTERN2$/{A=$1;for(i=2;i<NF;i++){A=A OFS toupper(substr($i,1,1)) substr($i,2)};print A OFS $NF;A="";next} {print}'  Input_file

Output will be as follows.
Code:
blank">PATTERN1 some word PATTERN2
title=">PATTERN1 Some Word PATTERN2
blank">PATTERN1 another word PATTERN2
title=">PATTERN1 Another Word PATTERN2
blank">PATTERN1 one more time PATTERN2
title=">PATTERN1 One More Time PATTERN2

On a Solaris/SunOS system, change awkto /usr/xpg4/bin/awk, /usr/xpg6/bin/awk , or nawk.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 02-11-2016
Thank you Aia and R.Singh.

Both solutions work great but i ran into a small issue. It seems to replace only the first occurrence and ignores the others.

I tried the following sample code with tokyo city, london and new york which i needed in Title Case.

Code:
title=">PATTERN1 tokyo city PATTERN2 whatever whatever title=">PATTERN1 london PATTERN2 whatever whatever title=">PATTERN1 new york PATTERN2 whatever whatever ....

I only got Tokyo City in Title case
Code:
title=">PATTERN1 Tokyo City PATTERN2 whatever whatever title=">PATTERN1 london PATTERN2 whatever whatever title=">PATTERN1 new york PATTERN2 whatever whatever ....

I was hoping to get every single occurrence between those patterns in Title case like: Tokyo City, London and New York
Code:
title=">PATTERN1 Tokyo City PATTERN2 whatever whatever title=">PATTERN1 London PATTERN2 whatever whatever title=">PATTERN1 New York PATTERN2 whatever whatever ....

Any ideas ? Thank you for your time.
# 6  
Old 02-11-2016
Will your input ever contain the string title=">PATTERN1 without a matching string PATTERN2 on the same line, such as one of the following?:
Code:
title=">PATTERN1 Tokyo City whatever whatever title=">PATTERN1 London PATTERN2 whatever whatever

or:
Code:
title=">PATTERN1 Tokyo City PATTERN2 whatever whatever title=">PATTERN1 London whatever whatever.

This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 02-11-2016
Quote:
Originally Posted by Don Cragun
Will your input ever contain the string title=">PATTERN1 without a matching string PATTERN2 on the same line, such as one of the following?:
Code:
title=">PATTERN1 Tokyo City whatever whatever title=">PATTERN1 London PATTERN2 whatever whatever

or:
Code:
title=">PATTERN1 Tokyo City PATTERN2 whatever whatever title=">PATTERN1 London whatever whatever.

Hi Don,

Yes it will, and also where sometimes
Code:
PATTERN2

appears before
Code:
title=">PATTERN1

Example:
Code:
PATTERN2 whatever whatever ..... title=">PATTERN1 tokyo city PATTERN2 whatever whatever title=">PATTERN1 London PATTERN2 whatever whatever .....title=">PATTERN1 whatever..........

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert text to lower case except the strings within single quotes

Shell : bash that comes with RHEL 6.7 I have SQL scripts like below. I want to convert all the text in these files to lower case except the strings enclosed within single quotes . Any idea how I can achieve this ? Sample text: $ cat sample.txt SELECT ... (6 Replies)
Discussion started by: John K
6 Replies

2. Shell Programming and Scripting

Help need to convert bi-lingual files in sub-title format

I have a large number of files in the standard subtitle format with the additional proviso that the files are bi-lingual i.e. English and a second language: in this case Hindi. A small sample is given below: 00 04 07 08 00 04 11 00 I mean very high fever... He even vomited. 00 04 07 08 00... (6 Replies)
Discussion started by: gimley
6 Replies

3. UNIX for Advanced & Expert Users

Shell script to convert words to Title case

Hi :) I have a .txt file with thousands of words. I was wondering if i could use a simple sed or awk command to convert / replace all words in the text file to Title Case format ? Example: from: this is line one this is line two this is line three to desired output: This Is Line... (8 Replies)
Discussion started by: martinsmith
8 Replies

4. UNIX for Dummies Questions & Answers

To convert Lower case to Upper Case

There is a script where we pass the parameter in lower case: say: . ./scriptName pArameter #!/bin/ksh echo "`date` Entering $0 Reloading the $1 table " mname1=$1 (code to login MYSQL Database) Truncate table $mname1; exit ! Since now there is a limitaion of MYSQL that it accept... (5 Replies)
Discussion started by: ambarginni
5 Replies

5. Shell Programming and Scripting

Matching and replacing text with case transformations

Hi, I am trying to edit an XML file automatically but my regex and shell script knowledge is very limited. I would appreciate your help fellows. The XML file has this structure: <?xml version="1.0" encoding="UTF-8"?> <map map_file="maps/world.swf" zoom="350%" zoom_x="-115%"... (1 Reply)
Discussion started by: boonymagique
1 Replies

6. Shell Programming and Scripting

Perl - Title Case after apostrophe

I've got: $string =~ s/(\w+)/\u\L$1/g; Which capitalizes each word in the string. The problem is if I have a string with an apostrophe the first letter after it gets capitalized as well. So Bob's becomes Bob'S. Thanks for any quick fixes! (4 Replies)
Discussion started by: mjmtaiwan
4 Replies

7. Shell Programming and Scripting

Script to Convert Upper case to Lower case

Hi All I have a script which extracts values from a Database (A persons name) and puts it into a variable in my script IE: $NAME However the Value in the DB is all in uppercase and contains the users first name and last name EG: > echo $NAME GRAHAM BOYLE > What I need is only the... (7 Replies)
Discussion started by: grahambo2005
7 Replies

8. Shell Programming and Scripting

convert upper case to lower case in ascript

I have a package to install and the installation script which does it . The files/directories names in the script are all lower case but the actual package has everything in upper case - file names, directories . I don't want to rename directories and files in the package - it has a lot of them . ... (2 Replies)
Discussion started by: vz6zz8
2 Replies

9. Shell Programming and Scripting

how to convert value in a variable from upper case to lower case

Hi, I have a variable $Ctrcd which contains country names in upper case and i want to convert them into lower case. I have tried so many solutions from already existing threads but couldn't get the correct one. Can anybody help me with this..... Thanks a lot.. (2 Replies)
Discussion started by: manmeet
2 Replies

10. UNIX for Advanced & Expert Users

Shell script to convert to Title case

I need a shell script which will convert the given string to Title case. E.g "hi man" to "Hi man" (5 Replies)
Discussion started by: SankarV
5 Replies
Login or Register to Ask a Question