![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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! |
|
|||||
|
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
$
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);}'
Last edited by Perderabo; 03-09-2008 at 04:29 PM.. Reason: I had my K and L reversed in the upper string! |
|
||||
|
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! |
|
|||||
|
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 "";}'
Code:
$ ./awkcap I Long John Silver $ |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|