LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-30-2020, 09:27 PM   #1
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,779

Rep: Reputation: 613Reputation: 613Reputation: 613Reputation: 613Reputation: 613Reputation: 613
sed to UPPERCASE specified characters


Have: a file in which every line contains a single word.
Have: n, a positive integer.

Want: an OutFile identical to the InFile except
the n-th character in every word is made UPPER CASE.

With this InFile ...
Code:
hudson
mercury
nash
oldsmobile
packard
plymouth
pontiac
studebaker
... this sed ...
Code:
n=3
sed -r "s/(.{$(($n-1))})(.)(.*$)/\1\u\2\3/" $InFile >$OutFile
... produced this OutFile ...
Code:
huDson
meRcury
naSh
olDsmobile
paCkard
plYmouth
poNtiac
stUdebaker
As shown, this is a solved problem.

The next step is to effect the same transformation on a file with multi-word lines.

With this InFile ...
Code:
hudson hornet
mercury marquis
nash rambler
oldsmobile omega
packard patrician
plymouth valiant
pontiac bonneville
studebaker lark
... the desired OutFile is ...
Code:
huDson hoRnet
meRcury maRquis
naSh raMbler
olDsmobile omEga
paCkard paTrician
plYmouth vaLiant
poNtiac boNneville
stUdebaker laRk
Can this be done with a single sed? Please advise.

Daniel B. Martin
 
Old 08-31-2020, 02:00 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 15,097

Rep: Reputation: 4972Reputation: 4972Reputation: 4972Reputation: 4972Reputation: 4972Reputation: 4972Reputation: 4972Reputation: 4972Reputation: 4972Reputation: 4972Reputation: 4972
"s/(\w{$(($n-1))})(\w)(\w*)/\1\u\2\3/g"
 
2 members found this post helpful.
Old 08-31-2020, 06:48 AM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,779

Original Poster
Rep: Reputation: 613Reputation: 613Reputation: 613Reputation: 613Reputation: 613Reputation: 613
Quote:
Originally Posted by pan64 View Post
"s/(\w{$(($n-1))})(\w)(\w*)/\1\u\2\3/g"
Excellent! Perfect! Thank you!

Daniel B. Martin

.
 
Old 08-31-2020, 09:01 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,795

Rep: Reputation: 3063Reputation: 3063Reputation: 3063Reputation: 3063Reputation: 3063Reputation: 3063Reputation: 3063Reputation: 3063Reputation: 3063Reputation: 3063Reputation: 3063
Code:
awk -F "" -vval=$n '$val = toupper($val)' OFS="" inFile
 
  


Reply

Tags
sed, text processing


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Omit specified characters from the output of a command Kenny_Strawn Programming 6 11-19-2010 04:07 AM
Keep last 'n' characters of a specified field danielbmartin Linux - Newbie 24 04-11-2010 04:40 PM
need sed to output block of text if specified text is matched r0dr1g0 Programming 3 02-13-2010 05:59 PM
why cannot create user account with uppercase characters on redhat EL3 ? hector48 Red Hat 3 07-15-2005 04:04 PM
delete line specified by variable wihin shel script with sed lnchatterbox Linux - Newbie 2 02-23-2004 01:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration