The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sed string manipulation speedieB Shell Programming and Scripting 4 11-23-2008 08:27 PM
string manipulation james6 UNIX for Dummies Questions & Answers 5 06-03-2008 11:05 AM
string manipulation Cactus Jack Shell Programming and Scripting 9 02-14-2008 01:14 PM
string manipulation hai1973 Shell Programming and Scripting 13 08-20-2007 12:27 PM
awk string manipulation zoo591 Shell Programming and Scripting 2 08-09-2006 01:13 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-09-2008
shadow0001 shadow0001 is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 10
String Manipulation Help

Hey Guys,

Right i know how to alter a word to begin with a capital letter, i know how to remove unwanted characters and replace them with the relevant character however i don't now if there is a way to do them all in one line.

Code:

echo -n ${string:0:1} | tr a-z A-Z #convert first letter in string to cap
echo ${string:1} # to remove first char & replace with cap from above
echo ${string/<char>/<char>} # to replace a character

What i would like to know, is there a way i can Join the second statement with the first .. So i can begin at the string from the second letter and at the same time omit unwanted characters.

Thanks in advance!
  #2 (permalink)  
Old 03-09-2008
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,124
Nobody ever specifies which language. Oh well, everybody uses ksh exclusively, right?
Code:
$ cat cap
#! /usr/bin/ksh

typeset -L1 -u first
name="henry"
first=$name
newname=${first}${name#?}
echo $name $newname
$ ./cap
henry Henry
$
  #3 (permalink)  
Old 03-09-2008
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,124
To get it to one line, I used awk...
Code:
$ cat awkcap
#! /usr/bin/ksh

L="abcdefghijklmnopqrstuvwxyz"
U="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
echo 'henry' | awk -v u=$U -v l=$L '{print substr(u,index(l,substr($0,1,1)),1) substr($0,2);}'
$ ./awkcap
Henry
$
The L and U variables could disappear and you could put the strings directly into the awk statement if you feel that I cheated by using those variables. But that makes for a very long statement.
Code:
#! /usr/bin/ksh

echo 'henry' | awk -v u="ABCDEFGHIJKLMNOPQRSTUVWXYZ" -v l="abcdefghijklmnopqrstuvwxyz" '{print substr(u,index(l,substr($0,1,1)),1) substr($0,2);}'
I prefer the first version, but the second is strictly "one line" as requested.

Last edited by Perderabo; 03-09-2008 at 04:29 PM.. Reason: I had my K and L reversed in the upper string!
  #4 (permalink)  
Old 03-09-2008
shadow0001 shadow0001 is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 10
Yep i knew i forgot to mention something, I'm using bash for this script. That probably means that the code there wont be work for me. After having a break I'm gonna get back on it know to see if i can do anything.

An example of what I'm trying to do (ill use your name example):

string is john#long.smith

So the currently my code removes unwanted characters leaving white spaces in their place, changes the first character to caps however i would want to change each new word to begin with a cap.

so output: John Long Smith - hope you can help! Ill be working on it in the mean time and get back if i find something.

Thanks!
  #5 (permalink)  
Old 03-09-2008
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,124
I edited my earlier post to fix a couple of letters that needed swapping. At this point you are asking a lot for one line of code! But...
Code:
#! /usr/bin/ksh

L="abcdefghijklmnopqrstuvwxyz"
U="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
echo 'i%%%%long $$$john&&&&&&  ((())) silver' |\
sed 's/[^a-zA-Z]/ /g;s/  */ /g'| awk -v u=$U -v l=$L '{for (i=1;i<=NF;i++)printf "%s%s ",substr(u,index(l,substr($i,1,1)),1),substr($i,2);print "";}'
seems to be working. I retreated to my preferred syntax where the long constants are out of the awk script.You can put them back if you really want to keep it to one line. Test run:
Code:
$ ./awkcap
I Long John Silver
$
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:52 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0