replace first character of string sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace first character of string sed
# 1  
Old 08-03-2007
replace first character of string sed

I want to change the first/or any character of a string to upper-case:

String:

test

Desired results:

Test or tEst or teSt or tesT

thanks
# 2  
Old 08-03-2007
Finally i could get only this

Code:
echo $(echo test | cut -c1 | tr '[a-z]' '[A-Z]')$(echo test | cut -c2-)

Might be others have different ideas...
# 3  
Old 08-03-2007
Quote:
Originally Posted by lorcan
Finally i could get only this

Code:
echo $(echo test | cut -c1 | tr '[a-z]' '[A-Z]')$(echo test | cut -c2-)

Might be others have different ideas...
thanks, just tried an awk example that works for the first character as well

Code:
bash-2.03$ echo test |awk '
BEGIN { upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
lower = "abcdefghijklmnopqrstuvwxyz"
}
{
FIRSTCHAR = substr($1, 1, 1)
if (CHAR = index(lower, FIRSTCHAR))
$1 = substr(upper, CHAR, 1) substr($1, 2)
print $0
}'
Test

# 4  
Old 08-03-2007
Code:
$echo test | perl -pe 's/^(.{0})(.)/$1\U$2/;'
Test
$echo test | perl -pe 's/^(.{1})(.)/$1\U$2/;'
tEst
$echo test | perl -pe 's/^(.{2})(.)/$1\U$2/;'
teSt
$echo test | perl -pe 's/^(.{3})(.)/$1\U$2/;'
tesT

# 5  
Old 08-03-2007
A plain unix commands solution would be lengthy.
Kahuna's solution is very short and easy to understand
even without knowledge of 'perl'. Smilie
# 6  
Old 08-03-2007
Quote:
Originally Posted by Shell_Life
A plain unix commands solution would be lengthy.
Kahuna's solution is very short and easy to understand
even without knowledge of 'perl'. Smilie
I agree as well, but sometimes perl packages are not always installed if you are running stripped down OSes.
# 7  
Old 08-03-2007
A shell version
Code:
$cat  example
#!/bin/sh
INPUT=test
for COL in 1 2 3 4
do
UPPER=`echo "$INPUT" | cut -c $COL  |tr '[a-z]' '[A-Z]'`
echo "$INPUT" | sed 's/./'$UPPER'/'$COL
done

$./example       
Test
tEst
teSt
tesT
$

Note: sed only will go up to 511 characters

Last edited by kahuna; 08-03-2007 at 03:37 PM.. Reason: Add Caviat
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed searches a character string for a specified delimiter character, and returns a leading or traili

Hi, Anyone can help using SED searches a character string for a specified delimiter character, and returns a leading or trailing space/blank. Text file : "1"|"ExternalClassDEA519CF5"|"Art1" "2"|"ExternalClass563EA516C"|"Art3" "3"|"ExternalClass305ED16B8"|"Art9" ... ... ... (2 Replies)
Discussion started by: fspalero
2 Replies

2. Shell Programming and Scripting

How to replace with a special character in String

Hi, I am beginner to Shell Scripting. I have a String like this "testabcdef", i need the first character as it is and the remaining character should be replaced by the the '*' character. e.g(t***********) PLZ Suggest me. (5 Replies)
Discussion started by: nanthagopal
5 Replies

3. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

4. Shell Programming and Scripting

replace a character in a string-help please

I have a string eg. word=promise and I have the masked version of the word which is -------(7dash for each character) if the user input a character O, I want the masked version of the word to be --o---- if the user inputs another character p, then the masked word becomes p-o---- How can i... (1 Reply)
Discussion started by: rajugurung
1 Replies

5. Shell Programming and Scripting

Replace character in certain position in a string

Hello everyone this is my first post of many to come :) I am writing a script and in this script at one point i need to replace a character in a particular position in a string for example: in the string "mystery" i would need to replace the 3rd position to an "r" so the string becomes... (3 Replies)
Discussion started by: snipaa
3 Replies

6. Shell Programming and Scripting

replace character from string

I have a string ----- i want to replace the 3rd '-' with a character How can i do that Basically im trying to do hangman I have added the '-----' string to a file called hash and i have the character in a variable called $input also i have the character location(index) in this variable... (12 Replies)
Discussion started by: omaral
12 Replies

7. Shell Programming and Scripting

In Sed how can I replace starting from the 7th character to the 15th character.

Hi All, Was wondering how I can do the following.... I have a String as follows "ACCTRL000005022RRWDKKEEDKDD...." This string can be in a file called tail.out or in a Variable called $VAR2 Now I have another variable called $VAR1="000004785" (9 bytes long), I need the content of... (5 Replies)
Discussion started by: mohullah
5 Replies

8. Linux

How to replace the 2nd character in a string using sed?

Hi, i have string var1=NN. Based on conditions, i have to change this first N to Y or second N to Y and send the details to other process. How to do that? This is a linux machine. Thanks, Selva (1 Reply)
Discussion started by: bharathappriyan
1 Replies

9. UNIX for Dummies Questions & Answers

Find and replace character in a string

Hi all, My problem is the following: I've a script that must list all files in a directory and write this information in a text file. I've tried to get the list through ls command and then write it using msgecho msgecho "`ls $PATH_APS_JOB_ORA`" This works good but the created string... (7 Replies)
Discussion started by: callimaco0082
7 Replies
Login or Register to Ask a Question